gorename: easy refactoring tool for Golang[转]
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[转]的更多相关文章
- Golang资料集
<Platform-native GUI library for Go> 介绍:跨平台的golang GUI库,支持Windows(xp以上),Unix,Mac OS X(Mac OS X ...
- Go语言(golang)开源项目大全
转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析 ...
- [转]Go语言(golang)开源项目大全
内容目录 Astronomy 构建工具 缓存 云计算 命令行选项解析器 命令行工具 压缩 配置文件解析器 控制台用户界面 加密 数据处理 数据结构 数据库和存储 开发工具 分布式/网格计算 文档 编辑 ...
- Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目
Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...
- golang 微服务以及相关web框架
golang 中国gocn golang Applicable to all database connection pools xorm是一个简单而强大的Go语言ORM库,通过它可以使数据库操作非常 ...
- [golang note] 环境搭建
LiteIDE(windows) • golang安装 ▶ 下载对应操作系统的版本并安装,下载地址:http://www.golangtc.com/download,譬如这里下载的是go1.6.win ...
- Visual Studio Code配置GoLang开发环境
Visual Studio Code配置GoLang开发环境 在Visual Studio Code配置GoLang开发环境 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页: ...
- The .NET of Tomorrow
Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciting times lie ahead f ...
- GO语言的开源库
Indexes and search engines These sites provide indexes and search engines for Go packages: godoc.org ...
随机推荐
- linux——DNS服务器配置
讲课,请不要在课堂上查找文件,浏览器搜索,会感觉你很不上心,玩听众,一定提前做很多遍,模拟很多遍: 演讲,请务必准备好材料,考虑听众的感受,一定不要让外行人云里雾里,听不懂你在讲什么,那就尴尬了, D ...
- 【344】Jupyter relevant problems
参考:Jupyter Notebook Tutorial: The Definitive Guide 参考:ipython notebook 如何修改一开始打开的文件夹路径? Ref: Install ...
- java mysql大数据量批量插入与流式读取分析
总结下这周帮助客户解决报表生成操作的mysql 驱动的使用上的一些问题,与解决方案.由于生成报表逻辑要从数据库读取大量数据并在内存中加工处理后在 生成大量的汇总数据然后写入到数据库.基本流程是 读取- ...
- SQL Server:查看数据库用户权限(SQL 2005)
1. 查看 SQL 2005 用户所属数据库角色 use yourdb go select DbRole = g.name, MemberName = u.name, MemberSID = u.si ...
- webdriver屏幕截图(python)
webdriver对当前页面进行截图,截取的是当前页面的全图,不论页面有多长,有两种截图方法 1.get_screenshot_as_file(XXX) 2.save_screenshot(XXX) ...
- java内存模型:简单理解
1.Java内存模型(Java Memory Model,JMM) 2.JMM定义了线程和主内存之间的抽象关系: 线程之间的共享变量存储在主内存(main memory)中,每个线程都有一个私有的本地 ...
- 每天学一点儿HTML5的新标签
sections部分 (http://www.w3.org/TR/html5/sections.html) 标签:article article标签用来表示页面中一段完整的可以自我包含的片段,具有可重 ...
- catkin_make 与cmake
http://blog.csdn.net/zyh821351004/article/details/50388429 1. catkin_make 与cmake的关系 程序在cmake编译的流程: ...
- php使用jquery Form ajax 提交表单,并上传文件
在html5中我们通过FormData就可以ajax上传文件数据,不过因为兼容问题.我们选用jquery.form.min.js来进行ajax的表单提交. 一.jquery.form.js下载地址 ...
- win下安装composer
1.在官网上下载composer的安装程序. https://getcomposer.org/ 2.双击运行Composer-Setup.exe程序,选择你自已的php安装目录打到php.exe. ...