gorename: easy refactoring tool for Golang

sunshine-anycall · · 3230 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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/


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:sunshine-anycall

查看原文:gorename: easy refactoring tool for Golang

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3230 次点击  
加入收藏 微博
上一篇:Go环境搭建
1 回复  |  直到 2000-01-01 00:00:00
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传