46

go imports添加

 5 years ago
source link: https://studygolang.com/articles/15210?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

go imports包的引入可以让代码书写变得更加快捷

一、如果有VPN,直接使用命令

go get -g -v golang.org/x/tools/cmd/goimports

二、网络不通的情况可以使用一下操作

打开一个terminal,到达goroot下(echo $GOROOT),运行

go get github.com/gpmgo/gopm

(原理是从镜像里获得) 下载结束后用git命令进行安装 然后再进入到GOPATH中找到github.com/gpmgo包,运行gopm即可:

go gopm get -g -v golang.org/x/tools/cmd/goimports

(参数-g是下载包到本地GOPATH中,-v是显示下载情况) 再次回到GOPATH中,src目录就可以看到golang.org目录,进入后就可以看到/x/tools/cmd/目录了

接下来就是go build工作了:

go install golang.org/x/tools/cmd/goimports

生成一个goimports的可执行文件(在GOROOT下面的bin目录中echo $GOROOT)

接下来就是IDE的配置了,这里以goland为例:

1.打开goland,打开settings(Ctrl+Alt+s);

2.打开tools中的File Watchers;

3.添加goimports进去即可

UZNZFzZ.png!web

(不知道能Markdown是否能放图···)

4.Apply后,ok,在goland菜单栏中找到Tools,点开后有一个Go Tools->选择goimports即可


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK