To inaugurate this attempt of blog, I’ll talk about gorename a small but incredibly useful tool I just recently discovered thanks to Andrew Gerrand’s dotGo 2014 talk. Gorename’s purpose is simple yet extremely useful, it allows us to safely rename constants, functions, variables and types in Go code. Gorename is code-aware, which means you don’t need to worry about renaming the wrong thing or unintended side effects.

Like for most Go tools, to install gorename we just need to “go get” it and, if we wish, move it to our bin folder of choice.

➜  go get golang.org/x/tools/cmd/gorename
➜ sudo mv bin/gorename /usr/local/bin #optional

Once we have it available in our $PATH, the usage could not be more simple. To showcase the usage we are going to make a modification to hugo the Go software that powers this blog. Let’s go get it.

➜  cd /tmp/hugo # better experiment safely
➜ export GOPATH=$PWD # on a temporary workspace
➜ go get github.com/spf13/hugo

Hugo is based on static files and watches for filesystem changes to reload your blog as you are updating it. To do so it uses the popular Chris Howey’s fsnotify library. It does so by getting a new watcher though thefsnotify.NewWatcher function on the hugo/watcher package.

Now let’s say that for whatever reason you want to change that function name from NewWatcher to something more specific like NewFileSystemWatcher. Your first instinct might be to run an old fashion search&replace over all the project files, don’t! You never know which other functions might share that name. And indeed hugo implements its own NewWatcher for higher level purposes, so you might want to keep that one intact.

This is when gorename comes in handy. To rename that function, and that one only across all the project, we just need to run this one-liner.

➜ gorename -from '"github.com/howeyc/fsnotify".NewWatcher' -to NewFileSystemWatcher
Renamed 4 occurrences in 4 files in 3 packages.

That’s it! gorename scanned our entire workspce and the function is renamed in both packages, fsnotify and hugo/watcher. Go into the folders and git diff for yourself. Notice the double quotes to indicate the package and the single quotes that wrap the function path.

Gorename supports multiple scoping options, your can rename a single variable on a single file. Now go take a look a the command’s help, I’m sure you can pick it up from here.

➜ gorename -help

....

A legal -from query has one of the following forms:

  "encoding/json".Decoder.Decode        method of package-level named type
(*"encoding/json".Decoder).Decode ditto, alternative syntax
"encoding/json".Decoder.buf field of package-level named struct type
"encoding/json".HTMLEscape package member (const, func, var, type)
"encoding/json".Decoder.Decode::x local object x within a method
"encoding/json".HTMLEscape::x local object x within a function
"encoding/json"::x object x anywhere within a package
json.go::x object x within file json.go For methods, the parens and '*' on the receiver type are both optional. ....

from:https://texlution.com/post/gorename/

gorename: easy refactoring tool for Golang[转]的更多相关文章

  1. Golang资料集

    <Platform-native GUI library for Go> 介绍:跨平台的golang GUI库,支持Windows(xp以上),Unix,Mac OS X(Mac OS X ...

  2. Go语言(golang)开源项目大全

    转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析 ...

  3. [转]Go语言(golang)开源项目大全

    内容目录 Astronomy 构建工具 缓存 云计算 命令行选项解析器 命令行工具 压缩 配置文件解析器 控制台用户界面 加密 数据处理 数据结构 数据库和存储 开发工具 分布式/网格计算 文档 编辑 ...

  4. Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目

    Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...

  5. golang 微服务以及相关web框架

    golang 中国gocn golang Applicable to all database connection pools xorm是一个简单而强大的Go语言ORM库,通过它可以使数据库操作非常 ...

  6. [golang note] 环境搭建

    LiteIDE(windows) • golang安装 ▶ 下载对应操作系统的版本并安装,下载地址:http://www.golangtc.com/download,譬如这里下载的是go1.6.win ...

  7. Visual Studio Code配置GoLang开发环境

    Visual Studio Code配置GoLang开发环境 在Visual Studio Code配置GoLang开发环境 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页: ...

  8. The .NET of Tomorrow

    Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciting times lie ahead f ...

  9. GO语言的开源库

    Indexes and search engines These sites provide indexes and search engines for Go packages: godoc.org ...

随机推荐

  1. 原生js实现三级复选框

    工作中要做一个三级的复选框,用js实现了一下,从项目中把相关代码抽取出来了,有相关需求的可以参考一下.亲测可用. <!DOCTYPE html> <html> <head ...

  2. CVE2016-8863libupnp缓冲区溢出漏洞原理分析及Poc

    1.libupnp问题分析: (1)问题简述: 根据客户给出的报告,通过设备安装的libupnp软件版本来判断,存在缓冲区溢出漏洞:CVE-2016-8863. (2)漏洞原理分析: 该漏洞发生在up ...

  3. mysql 导入csv 转义

    TERMINATED :分隔符 ESCAPED :转义用什么标示,‘’ 不设置转义符 LOAD DATA LOCAL INFILE '/home/tmp/1999/holder.csv'  INTO ...

  4. javascript中的二维数组

    要创建一个二位数组我们脑子里第一个出现的就是 var arr=[][]; 但是在javascript这样是会报错的,要在javascrip中创建一个二位数组对象方法如下 方法一     直接把数组写出 ...

  5. sql重复数据只取一条记录

    1.SQL SELECT DISTINCT 语句 在表中,可能会包含重复值.这并不成问题,不过,仅仅列出不同(distinct)的值. 关键词 DISTINCT 用于返回唯一不同的值. 语法: SEL ...

  6. easyUIDataGrid分页

    package com.cn.eport.util; import java.util.List; /** * * * @author zh * */ public class DataGrid im ...

  7. IsPostBack用法

    可以自己定义 在页面中定义隐藏的input,设置为ispostback. <form action="" method=""> <input ...

  8. hdoj1043 Eight(逆向BFS+打表+康拓展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 思路: 由于自己对康拓展开用的太少,看到这个题没想到康拓展开,最开始打算直接转换为数字,但太占内 ...

  9. hover

    hover - Bing dictionary US[ˈhɒvə(r)] v.盘旋:徘徊:犹豫:巡弋 网络翱翔:悬停:盘旋于

  10. 42-python中的矩阵、多维数组----numpy

    xzcfightingup   python中的矩阵.多维数组----numpy 1. 引言 最近在将一个算法由matlab转成python,初学python,很多地方还不熟悉,总体感觉就是上手容易, ...