go-gopath

前端之家收集整理的这篇文章主要介绍了go-gopath前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

提示:由于水平有限,如发现有疑问或错误的地方请毫不客气的提出、讨论,我会在第一时间回复,感谢在先

GOPATH can be any directory on your system. In Unix examples,we will set it to $HOME/go (the default since Go 1.8). Note that GOPATH must not be the same path as your Go installation. Another common setup is to set GOPATH=$HOME (mac上面如果没有设置gopath,默认是$HOME/go)

Bash中的GOPATH设置

Edit your ~/.bash_profile to add the following line:

export GOPATH=$HOME/go

Save and exit your editor. Then,source your ~/.bash_profile

source ~/.bash_profile

工作中可以指定 gopath 为当前工作目录

查看GOPATH

go env GOPATH 

设定GOPATH

export  GOPATH=$HOME/path_to_gopath  //建议使用这个
export GOPATH=$GOPATH:path_to_gopath
export GOPATH=$(go env GOPATH)

把工程下面可以执行文件添加到执行路径

export PATH=$PATH:$(go env GOPATH)/bin

VSC中关于GOPATH设置问题

At any point in time,you can run the command Go: Current GOPATH to see the GOPATH being used by the extension.(在VSC中查看当前Gopath环境 command + shift + p; 然后键入 Go: Current GOPATH)

GOPATH from the environment variable 环境变量

Out of the box (开箱即用),the extension uses the value of the environment variable GOPATH. From Go 1.8 onwards,if no such environment variable is set,then the default GOPATH as deciphered(解释,从....中获得) from the command go env is used.

GOPATH from go.gopath setting

Setting go.gopath in User settings(__用户设置__) overrides the GOPATH that was derived(解释,从...中获得) from the above logic. Setting go.gopath in Workspace settings(__工作空间__) overrides the one from User settings. You can set multiple folders(多个文件夹) as GOPATH in this setting. Note that they should be ; separated in Windows and : separated otherwise(在wins中多个GOPATH路径由";"进行分割,在其他的环境中由”:“进行分割).

GOPATH from go.inferGopath setting (infer : 推测)

Setting go.inferGopath overrides the value set in go.gopath setting. If go.inferGopath is set to true,the extension will try to infer(推断) the GOPATH from the path of the workspace i.e. the directory opened in vscode. It searches upwards in the path for the src directory,and sets GOPATH to one level above that. It will also include the global GOPATH(__glbal GOPATH 指的是 go env GOPATH __). Run go env GOPATH to find out what your global GOPATH is.(在当前打开的目录中向上寻找SRC目录,并在高一级的目录中设置GOPATH)

For example,if your project looks like /aaa/bbb/ccc/src/...,then opening the directory /aaa/bbb/ccc/src (or anything below that) will cause the extension to search upwards,find the src component(文件夹) in the path,and set the GOPATH to one level above that i.e. GOPATH=/aaa/bbb/ccc.

This setting is useful when you are working on different Go projects which have different GOPATHs. Instead of setting the GOPATH in the workspace settings of each project or setting all the paths as ;/: separated string(使用";"或者":"分割的多个GOPATH路径),you can just set go.inferGopath to true and the extension uses the right GOPATH automatically.

GOPATH for installing the Go tools using go.toolsGopath

By default,all the dependent Go tools are used from the GOPATH derived(解释,从...中获得) from the above logic. If they are available on your PATH,the PATH is used to locate the Go tools. If the Go tools are not in your path,you might end up with the same Go tools installed in each of your GOPATHs. To prevent the Go tools from cluttering(使凌乱) your GOPATH,use the go.toolsGopath setting to provide a separate(独立的) location for the Go tools.

The first time you set go.toolsGopath,you will have to run Go: Install Tools command so that the Go tools get installed in the provided location.

GOPATH while debugging

The debug adapter(调试器) in the Go extension does not have access to your User/Workspace settings(没有访问User/Workspace的权限). Therefore,the only GOPATH the debugger is aware(感知) of is the one set as environment variable outside of VS Code. If there is no such environment variable,then the debugger tries to guess the GOPATH by using the same logic as the inferGopath setting described above
编译器并不能访问
用户设置或者是工作区的设置,它唯一能够辨识的是VCS之外的GOPATH环境变量,如果没有设置该外部的GOPATH环境变量,那么调试器的行为将和inferGopath相同; 如果没有 GOPATH 环境变量,那么debug adapter将 按照上面描述的相同逻辑guess GOPATH

You can also provide GOPATH in the env property of the debug configuration Read more on debugging at

多人开发一些思考

结合VSC的功能我们可以指定当前的工作目录(workSpace)为本应用内的gopath,这样每一个workSpace都对应一个应用内的gopath,但是这个gopath并没有改变环境变量$GOPATH的值(go env GOPATH); 执行go install 以及go get 时候,可能会报错;因为没有在环境变量GOPATH的路径中执行代码; 假如即使代码执行成功,代码也是安装在$GOPATH中的bin,src中;

多人开发中的方法:

  • 1

    1. 如果是比较基础的库可以 直接 go get 下载到系统的$GOPATH路径中,这样其他工作目录(项目)使用的时候不用再下载了
    2. 如果是属于特定功能的包的话,需要临时更改一下 export GOPATH = workSpace,然后执行go install,go get ,当操作完成后再改回来. 这样这个包下载到当前的工作目录而不是下载到GOPATH默认的目录; 当前的工作目录可以添加到版本管理中,(对于其他的$GOPATH中的包,不需要添加到版本管理中); 这样的缺点是:其他同事还是需要go install 或者 go get 安装其他基础库(而且这些基础库搞不好就会下载到当前的工作目录中,这样就产生混乱);
  • 2
    把所有的库都下载到当前的工作目录中;方便管理; 下载的时候只需要临时更改一下$GOPAHT的值 (这样不会因为改变GOPATH而重新把VSC插件下载到对应的workSpace中);
    疑问: 为什么不把 $GOPATH 指向当前的工作目录? 考虑到如果在其他的项目或者demo中下载第三方库可能会误操作下载到当前工作目录中; 另外还要避免goTOOl 下载路径的问题

多人开发需要考虑的因素:

  1. 要考虑到第三方库的版本升级时,升级任务需要一人负责防止版本不一致;
  2. 是否操作简单,逻辑是否简单;
  3. 不同平台的不同开发工具代码管理

其他一些内容

问题:VSC中在同一包中,

文件布局

--src 
  --ch1
    --a.go
  --ch2
    --main
        --main.go
   a.go
   b.go

已知:a.go,b.go 属于ch2包,a.go 属于ch1包,main.go 属于 main 包;
main.go 不能和a.go,b.go 同级;因为ch2文件下不能包含两个包ch2 和 main,所以把main.go放在了main包下;

go 的扩展使用了go.inferGopath = true;测试ch2内容的时候直接 import "ch2" 即可,如果没有设置go.inferGopath,可以使用相对路径引入包 import "../../packageName"

如有一个包是远程仓库的包需要go get下来,然后再引入到import 路径中使用

已经在VSC中设置了 工作区的go.inferGopath;但是在VSC命令行中这个参数不起作用(如果在不同的目录可能会找不到包)

--src 
    --inter
            --main
                   main.go
    a.go
    b.go
    c.go
--otherpackages
如果在命令行中执行 go run main.go ; 而且main中包含inter包;就会报错
可以通过修改main.go inter包为相对路径可以解决;但是在VSC debug需要改过来.
</code></pre>

如何管理go get 下来的包文件(下载之后再拖进工程?),如何管理各个项目之间的go tools?

原文链接:https://www.f2er.com/note/413586.html

猜你在找的程序笔记相关文章