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/

时间: 2024-08-29 04:26:58

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

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

转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析器控制台用户界面加密数据处理数据结构数据库和存储开发工具分布式/网格计算文档编辑器Encodings and Character SetsGamesGISGo ImplementationsGraphics and AudioGUIs and Widget ToolkitsHardwareLangu

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

内容目录 Astronomy 构建工具 缓存 云计算 命令行选项解析器 命令行工具 压缩 配置文件解析器 控制台用户界面 加密 数据处理 数据结构 数据库和存储 开发工具 分布式/网格计算 文档 编辑器 Encodings and Character Sets Games GIS Go Implementations Graphics and Audio GUIs and Widget Toolkits Hardware Language and Linguistics 日志 机器学习 Math

golang 微服务以及相关web框架

golang 中国gocn golang Applicable to all database connection pools xorm是一个简单而强大的Go语言ORM库,通过它可以使数据库操作非常简便--文档 Lightweight and fast SQL builder for Go and XORM Go 语言包管理 A web based database admin tool written by Golang support mysql/postgres/sqlite Go 语言

VS Code Golang环境安装

1)安装gocode go get -u -v github.com/nsf/gocode 2)安装godef go get -u -v github.com/rogpeppe/godef 3)安装golint go get -u -v github.com/golang/lint/golint 4)安装go-find-references go get -u -v github.com/lukehoban/go-find-references 5)安装go-outline go get -u

awesome-php中英文资源整理(同步更新)

中文版 收集整理一些常用的PHP类库, 资源以及技巧. 以便在工作中迅速的查找所需… 这个列表中的内容有来自 awesome-php 的翻译, 有来自开发者周刊以及个人的积累等. 一个前端组件的列表 awesome-frontend 推荐 学习资源 PHP相关的有参考价值的社区,博客,网站,文章,书籍,视频等资源 PHP网站(PHP Websites) PHP The Right Way – 一个PHP实践的快速参考指导 PHP Best Practices – 一个PHP最佳实践 PHP We

GO语言的开源库

Indexes and search engines These sites provide indexes and search engines for Go packages: godoc.org gowalker gosearch Sourcegraph Contributing To edit this page you must be a contributor to the go-wiki project. To get contributor access, send mail t

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciting times lie ahead for .NET developers. During Build, Microsoft’s biggest developer event of the year, clear roadmaps were given on the future of .NET and C#. Microsoft i

Know Your IDE

Know Your IDE Heinz Kabutz iN THE 1980S, our programming environments were typically nothing bet- ter than glorified text editors-if we were lucky. Syntax highlighting, which we take for granted nowadays, was a luxury that certainly was not available

.NetChajian

Code generation(代码自动生成) NVelocity CodeSmith X-Code .NET XGoF - NMatrix / DEVerest Compilation(编译工具) eXtensible C# - ResolveCorp Mono DotGNU - GNU Obfuscation(混淆加密) LSW-IL-Obfuscator - Lesser Software Demeanor for .NET - Wise Owl Salamander .NET Obfus