博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
gorename: easy refactoring tool for Golang[转]
阅读量:6278 次
发布时间:2019-06-22

本文共 3079 字,大约阅读时间需要 10 分钟。

o inaugurate this attempt of blog, I’ll talk about gorename a small but incredibly useful tool I just recently discovered thanks to . 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  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  library. It does so by getting a new watcher though the 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  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 NewFileSystemWatcherRenamed 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
你可能感兴趣的文章
CCNP-6 OSPF试验2(BSCI)
查看>>
Excel 2013 全新的图表体验
查看>>
openstack 制作大于2TB根分区自动扩容的CENTOS镜像
查看>>
Unbuntu安装遭遇 vmware上的Easy install模式
查看>>
几个常用的ASP木马
查看>>
python分析postfix邮件日志的状态
查看>>
Mysql-5.6.x多实例配置
查看>>
psutil
查看>>
在git@osc上托管自己的代码
查看>>
机器学习算法:朴素贝叶斯
查看>>
小五思科技术学习笔记之扩展访问列表
查看>>
使用Python脚本检验文件系统数据完整性
查看>>
使用MDT部署Windows Server 2003 R2
查看>>
Redhat as5安装Mysql5.0.28
查看>>
通过TMG发布ActiveSync
查看>>
Web服务器的配置与管理(4) 配置访问权限和安全
查看>>
爆牙齿的Web标准面试考题II(iPhone SMS/iChat UI的Web标准实现)
查看>>
XMOVE3.0手持终端——软件介绍(二):在2KB内存的单片机上实现的彩屏GUI控件库
查看>>
MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)
查看>>
找小于N 的所有质数
查看>>