What does go install do?

huangapple go评论85阅读模式
英文:

What does go install do?

问题

文档中没有提到buildinstall的具体作用。

我原本的期望是它类似于make install,即将编译好的内容放置在最终位置(例如/usr/local/bin/my_new_toy),但似乎它将文件放置在GOROOT/bin中。

我能否让Go执行make install,即将文件放置在其他位置?还是我需要编写一个makefile(请告诉我不需要)?

英文:

The docs say nothing about what build vs install does

My expectation was that it's like make install; i.e. 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

Can I tell go to do a make install - i.e. put things elsewhere? Or do I just write a makefile (please tell me no)?

答案1

得分: 115

go build vs go install:

> go build只编译可执行文件并将其移动到目标位置。
> go install做了更多的事情。它将可执行文件移动到$GOPATH/bin,并缓存所有被导入到$GOPATH/pkg的非主要包。只要源代码没有改变,下次编译时将使用缓存。


go buildgo install后的包树结构:

.
├── bin
│   └── hello  # 通过go install生成
└── src 
    └── hello
        ├── hello  # 通过go build生成
        └── hello.go

更详细的信息

英文:

go build vs go install:

> go build just compiles the executable file and moves it to the destination.
> go install does a little bit more. It moves the executable file to
> $GOPATH/bin and caches all non-main packages which are imported to
> $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:

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

More detailed information.

答案2

得分: 98

如果你想让二进制文件放在特定的位置,你可以使用环境变量 GOBIN

bin/ 目录保存了编译后的命令。每个命令的名称都是根据其源代码目录命名的,但只包含最后一个元素,而不是整个路径。也就是说,源代码位于 DIR/src/foo/quux 的命令将被安装到 DIR/bin/quux,而不是 DIR/bin/foo/quux。foo/ 被去掉了,这样你就可以将 DIR/bin 添加到 PATH 中以便使用已安装的命令。如果设置了 GOBIN 环境变量,命令将被安装到指定的目录,而不是 DIR/bin。

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

GOBIN=/usr/local/bin/ go install

如果你想要每个项目都有一个 bin/ 目录,你可以简单地将项目路径追加到 GOPATH 中,但是你的代码必须位于 $project-path/src/ 下,go install 将把所有的二进制文件放在 $project-path/bin 中。

export GOPATH=/dir1:/dir2:/dir3

如果未设置 GOBIN,来自 /dir1/src 的二进制文件将放在 /dir1/bin 中,来自 /dir2/src 的二进制文件将放在 /dir2/bin 中,依此类推(来自 $GOROOT/src 的二进制文件将放在 $GOROOT/bin 中)。

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

你也可以使用以下方式(感谢 JimB):

go build -o /path/binary-name
英文:

If you want binary files to go to a specific location, you can use the environment variable GOBIN :

> The bin/ directory holds compiled commands. 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. The foo/ is stripped so that you
> can add DIR/bin to your PATH to get at the installed commands. If the
> GOBIN environment variable is set, commands are installed to the
> directory it names instead of DIR/bin.

Source : 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.

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).

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

And you can also just use (thanks JimB):

go build -o /path/binary-name

huangapple
  • 本文由 发表于 2014年6月6日 04:27:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/24069664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定