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:
Save and exit your editor. Then,source your ~/.bash_profile
工作中可以指定 gopath 为当前工作目录 查看GOPATH
设定GOPATH
把工程下面可以执行文件添加到执行路径
VSC中关于GOPATH设置问题At any point in time,you can run the command
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 settingSetting 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.toolsGopathBy 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 GOPATH while debuggingThe 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 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中; 多人开发中的方法:
多人开发需要考虑的因素:
其他一些内容问题:VSC中在同一包中, 文件布局
已知:a.go,b.go 属于ch2包,a.go 属于ch1包,main.go 属于 main 包; go 的扩展使用了go.inferGopath = true;测试ch2内容的时候直接 import "ch2" 即可,如果没有设置go.inferGopath,可以使用相对路径引入包 import "../../packageName" 如有一个包是远程仓库的包需要go get下来,然后再引入到import 路径中使用 已经在VSC中设置了 工作区的go.inferGopath;但是在VSC命令行中这个参数不起作用(如果在不同的目录可能会找不到包)
|