如何使用Golang软件包以外的名称构建可执行文件 [英] How to build executable with name other than Golang package
问题描述
如果我的Golang软件包名称是以下内容之一,是否可以构建(安装,运行get等)名称为foobar
的可执行文件:
Is it possible to build (install, go get, etc) an executable with the name foobar
if my Golang package name is one of the following:
-
github.com/username/go-foobar
-
github.com/username/foobar-tools
github.com/username/go-foobar
github.com/username/foobar-tools
并且软件包根目录中有main.go
吗?
and has main.go
in the package root?
推荐答案
go build -o <your desired name>
您可以使用带有go build
的-o开关来指定可执行文件名称.以您的示例为例:
cd $GOPATH/github.com/username/go-foobar && go build -o foobar
.但是,您只剩下软件包文件夹中的可执行文件-您仍然需要以某种方式安装它.
You can specify the executable name using the -o switch with go build
. For your example it would look something like:
cd $GOPATH/github.com/username/go-foobar && go build -o foobar
. However, you're just left with the executable in the package's folder -- you still need to install it somehow.
但是,对于使用go get github.com/username/go-foobar
安装工具的用户,我不知道有什么方法可以指定它.例如,请参见以下答案: https://stackoverflow.com/a/33243591/2415176
However, I don't know of any way to specify that for someone using go get github.com/username/go-foobar
to install your tool. For instance, see this answer: https://stackoverflow.com/a/33243591/2415176
如果您不担心有人使用go get
安装工具,则可以将其包装在Makefile中.
If you're not worried about people installing your tool with go get
, this is the kind of thing you can wrap in a Makefile.
这篇关于如何使用Golang软件包以外的名称构建可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!