1. 云栖社区>
  2. 博客列表>
  3. 正文

gorename: easy refactoring tool for Golang[转]

ghost丶桃子 2016-05-18 19:15:48 浏览668 评论0

go, gorename

摘要: 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.

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
版权声明:本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

用云栖社区APP,舒服~

【云栖快讯】中办国办印发《推进互联网协议第六版(IPv6)规模部署行动计划》加快推进基于 IPv6 的下一代互联网规模部署,计划指出2025年末中国 IPv6 规模要达到世界第一,阿里云也第一时间宣布了将全面提供IPv6服务,那么在全面部署 IPV6 前,你需要了解都在这儿  详情请点击

网友评论

是为多媒体数据提供的转码计算服务。它以经济、弹性和高可扩展的音视频转换方法,将多媒体数据转码成适合在PC、TV以... 更多>

集音视频上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播解决方案。 更多>

支持 PB 级数据存储的海量分布式关系型数据库。它支持 MySQL 数据库接口,采用可扩展的分布式体系, 稳定可... 更多>

为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效率,降低 IT 成本... 更多>
订阅广场全新上线

订阅广场全新上线