gorename: easy refactoring tool for Golang[转]

简介:

o 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/

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/p/4801001.html
相关文章
|
15天前
|
Go
golang中make 和 new 的区别
golang中make 和 new 的区别
19 0
|
30天前
|
Rust Java Go
Python is Easy. Go is Simple. Simple != Easy
Python以其易学易用著称,常用于初学者编程和复杂科学计算,但其解释器的复杂性和环境易变性可能导致运行时问题。Go语言则追求简单,语法稳定,编译快速,生成的二进制文件小巧、独立。Go的静态链接特性使其能在不同系统上无缝运行,而Python在数据科学和原型设计上仍具有优势。结合两者,通过Django进行快速原型验证,然后用Go重构业务逻辑和高性能部分,形成了一种有效的开发策略。
18 0
|
NoSQL Java Go
记一次Golang内存分析——基于go pprof
## 1. 背景 阿里云Redis线上在某些任务流中使用`redis-port`来进行实例之间的数据同步。`redis-port`是一个MIT协议的开源软件,主要原理是从源实例读取RDB快照文件、解析、然后在目标实例上应用灌数据的写命令。为了限制每个进程的最大内存使用,我们使用cgroup来做隔离,最近线上出现redis-port在同步数据时`OOM`的情况,最高内存使用达到了`10G`以上
24708 0
|
3月前
|
Go
【golang】Compile 和 MustCompile
【golang】Compile 和 MustCompile
33 0
golang go get 时提示 no Go files in xxx
golang go get 时提示 no Go files in xxx
golang go get 时提示 no Go files in xxx
|
IDE Go 开发工具
让你的Golang项目在IDE里跑起来(Goland使用入门-GOROOT、GOPATH、src、 pkg、bin、import)
让你的Golang项目在IDE里跑起来(Goland使用入门-GOROOT、GOPATH、src、 pkg、bin、import)
让你的Golang项目在IDE里跑起来(Goland使用入门-GOROOT、GOPATH、src、 pkg、bin、import)
|
Java Linux Go
知识分享之Golang——go mod常用命令解析
知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进行共同学习。欢迎大家进行持续关注。 知识分享系列目前包含Java、Golang、Linux、Docker等等。
146 0
知识分享之Golang——go mod常用命令解析
|
Java Linux Go
知识分享之Golang——go get 常用命令
知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进行共同学习。欢迎大家进行持续关注。 知识分享系列目前包含Java、Golang、Linux、Docker等等。
214 0
知识分享之Golang——go get 常用命令
|
Go Windows
golang hello 安装环境异常【已解决】
golang hello 安装环境异常【已解决】
72 0
golang hello 安装环境异常【已解决】
|
Go 开发工具 Windows
GoLang的安装和使用
GoLang的安装和使用
290 2

热门文章

最新文章