如果我的 Golang 包名称是以下之一,是否可以构建(安装、获取等)名称为 foobar
的可执行文件:
github.com/username/go-foobar
github.com/username/foobar-tools
并且在包根目录中有 main.go
?
最佳答案
go build -o <your desired name>
您可以使用带有 go build
的 -o 开关指定可执行文件名称。对于您的示例,它看起来像:
cd $GOPATH/github.com/username/go-foobar && go build -o foobar
。然而,你只剩下包文件夹中的可执行文件——你仍然需要以某种方式安装它。
但是,我不知道有什么方法可以为使用 go get github.com/username/go-foobar
安装您的工具的人指定。例如,请参阅此答案:https://stackoverflow.com/a/33243591/2415176
如果您不担心人们使用 go get
安装您的工具,那么您可以将这种东西包装在 Makefile 中。
关于go - 如何使用 Golang 包以外的名称构建可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42706246/