您的位置:首页 > 运维架构

环境变量GOPATH使用试验

2018-09-10 15:58 381 查看
原文链接:http://www.cnblogs.com/luo630/p/9620278.html

Intel Core i5-8250U,Windows 10家庭中文版,Visual Studio Code 1.26.1

 

中午的时候,自己把环境变量GOBIN给清空了。刚刚继续工作时发生了异常:使用Visual Studio Code写的代码,无法使用go install编译运行(或生成可执行文件)。

package main

var x, y int
var (
a int
b bool
)

var c, d int = 1, 2
var e, f = 123, "hello"

func main() {
g, h := 321, "olleh"

println(x, y, a, b, c, d, e, f, g, h)
}
eg1.go

源文件位于项目的src下的app3文件夹中——D:\ws\golang\prj1\src\app3。

 

在终端进入prj1的src目录,执行go install .\app3,结果发生下面的错误——应用程序app3的目录位于GOPATH之外,,此时的GOPATH是默认值,只有一个目录,位于C:\users\<username>\go:

>go install .\app3
go install: no install location for directory D:\ws\golang\prj1\src\app3 outside GOPATH
For more details see: 'go help gopath'

进入app3的目录执行go install eg1.go也不行——提示没有设置GOBIN,因为自己前面清空了它:

app3>go install eg1.go
go install: no install location for .go files listed on command line (GOBIN not set)

 

在前面一篇博文的更新中了解到,设置了GOBIN可以解决第二个问题。那么,第一个问题怎么解决呢?

通过看一些关于GO环境变量的文章(参考链接),知道GOPATH可以设置多个值,好吧,把当前项目加入到GOPATH中,再测试一下吧!

 

注意,在这之前,自己尝试把eg1.go放到GOPATH下的src中的一个文件夹中,此时执行go install是 成功的,所以才会有上面的想法 和 本文。

 

在当前终端把eg1.go所在项目路径 附加到GOPATH中,再测试go install,结果,成功!可执行文件被放到了项目的bin文件中了。

D:\ws\golang\prj1>echo %GOPATH%
C:\Users\log\go

D:\ws\golang\prj1>set GOPATH=%GOPATH%;D:\ws\golang\prj1

D:\ws\golang\prj1>echo %GOPATH%
C:\Users\log\go;D:\ws\golang\prj1

D:\ws\golang\prj1>go install .\src\app3

D:\ws\golang\prj1>dir bin
驱动器 D 中的卷是 新加卷
卷的序列号是 BAD3-E0B3

D:\ws\golang\prj1\bin 的目录

2018/09/10  15:36    <DIR>          .
2018/09/10  15:36    <DIR>          ..
2018/09/10  15:04         1,021,440 app2.exe
2018/09/10  15:36         1,021,440 app3.exe

D:\ws\golang\prj1>.\bin\app3.exe
0 0 0 false 1 2 123 hello 321 olleh

 

上面执行成功!

 

前面提到的是两个错误:go install后跟文件夹名-失败,go install后跟源文件名-失败。上面设置GOPATH仅仅解决了第一个问题,那么,第二个问题怎么解决呢?

根据错误提示,设置GOBIN!GOBIN设置在哪里,go install后的可执行文件就出现在哪里!(前一篇博文的更新部分有介绍)。

 

查看go install的用法:其最后一个参数时packages(多个包吗?),而不是源文件,,这部分内容自己还没弄明白,后续再dig。

>go help install
usage: go install [-i] [build flags] [packages]

Install compiles and installs the packages named by the import paths.

The -i flag installs the dependencies of the named packages as well.

For more about the build flags, see 'go help build'.
For more about specifying packages, see 'go help packages'.

See also: go build, go get, go clean.

 

测试使用go install同时安装多个应用程序:果然可以!

D:\ws\golang\prj1\src>go install app1 app2 app3

D:\ws\golang\prj1\src>cd ..

D:\ws\golang\prj1>cd bin

D:\ws\golang\prj1\bin>dir
驱动器 D 中的卷是 新加卷
卷的序列号是 BAD3-E0B3

D:\ws\golang\prj1\bin 的目录

2018/09/10  15:47    <DIR>          .
2018/09/10  15:47    <DIR>          ..
2018/09/10  15:47         1,952,768 app1.exe
2018/09/10  15:47         1,021,440 app2.exe
2018/09/10  15:47         1,021,440 app3.exe
3 个文件      3,995,648 字节
2 个目录 78,701,293,568 可用字节

 

通过本次试验,自己也理解了Eclipse里面的一个Go配置项的意义了:Also add project location to GOPATH, ...

 

参考链接

1.GOROOT、GOPATH、GOBIN、project目录

 

后记

对于Go命令工具,自己还需要更熟悉才是。那样的话,可以少走不少弯路的。

下面的教程挺好的,需要看看:

GO 命令教程(http://wiki.jikexueyuan.com/project/go-command-tutorial/)

 

转载于:https://www.cnblogs.com/luo630/p/9620278.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: