简体   繁体   English

go install 有什么作用?

[英]What does go install do?

The docs say nothing about what build vs install does文档没有说明buildinstall作用

My expectation was that it's like make install ;我的期望是它就像make install ie it takes the compiled stuff and puts in its final location ( /usr/local/bin/my_new_toy or whatever) but it seems that it puts things in GOROOT/bin即它需要编译的东西并放入其最终位置( /usr/local/bin/my_new_toy或其他)但它似乎将东西放入GOROOT/bin

Can I tell go to do a make install - ie put things elsewhere?我可以告诉去做一个make install - 即把东西放在别处吗? Or do I just write a makefile (please tell me no)?或者我只是写一个makefile(请告诉我不是)?

go build vs go install: go buildgo install:

go build just compiles the executable file and moves it to the destination. go build只是编译可执行文件并将其移动到目的地。 go install does a little bit more. go install做的更多。 It moves the executable file to $GOPATH/bin and caches all non-main packages which are imported to $GOPATH/pkg .它将可执行文件移动到$GOPATH/bin并缓存导入到$GOPATH/pkg所有非主包。 The cache will be used during the next compilation provided the source did not change yet.如果源尚未更改,将在下一次编译期间使用缓存。


A package tree after go build and go install : go buildgo install后的包树:

.
├── bin
│   └── hello  # by go install
└── src 
    └── hello
        ├── hello  # by go build
        └── hello.go

More detailed information . 更详细的信息

If you want binary files to go to a specific location, you can use the environment variable GOBIN :如果您希望二进制文件转到特定位置,可以使用环境变量GOBIN

The bin/ directory holds compiled commands. bin/ 目录保存已编译的命令。 Each command is named for its source directory, but only the final element, not the entire path.每个命令都以其源目录命名,但只有最后一个元素,而不是整个路径。 That is, the command with source in DIR/src/foo/quux is installed into DIR/bin/quux, not DIR/bin/foo/quux.也就是说,在 DIR/src/foo/quux 中带有 source 的命令安装在 DIR/bin/quux 中,而不是 DIR/bin/foo/quux 中。 The foo/ is stripped so that you can add DIR/bin to your PATH to get at the installed commands. foo/ 被剥离,以便您可以将 DIR/bin 添加到您的 PATH 以获取已安装的命令。 If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin.如果设置了 GOBIN 环境变量,命令将安装到它命名的目录而不是 DIR/bin。

Source : http://golang.org/cmd/go/#hdr-GOPATH_environment_variable来源: http : //golang.org/cmd/go/#hdr-GOPATH_environment_variable

GOBIN=/usr/local/bin/ go install

If you want per-project bin/ directory then you can simply append your project path to GOPATH , however you must have your code under $project-path/src/ and go install will put all the binaries in $project-path/bin .如果您想要每个项目的bin/目录,那么您可以简单地将您的项目路径附加到GOPATH ,但是您必须将代码放在$project-path/src/并且go install会将所有二进制文件放在$project-path/bin

export GOPATH=/dir1:/dir2:/dir3

If GOBIN is not set, binaries from /dir1/src end up in /dir1/bin, binaries from /dir2/src end up in /dir2/bin, and so on (and binaries from $GOROOT/src end up in $GOROOT/bin).如果未设置 GOBIN,则 /dir1/src 中的二进制文件最终在 /dir1/bin 中,/dir2/src 中的二进制文件最终在 /dir2/bin 中,依此类推($GOROOT/src 中的二进制文件最终在 $GOROOT 中) /bin)。

Source :https://groups.google.com/forum/#!topic/golang-nuts/-mN8R_Fx-7M来源:https ://groups.google.com/forum/#!topic/golang- nuts/-mN8R_Fx-7M

And you can also just use (thanks JimB):你也可以只使用(感谢 JimB):

go build -o /path/binary-name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM