如何以 Golang 软件包以外的名称建立可执行文件

如果我的 Golang 软件包名是下列名称之一,是否可以构建(安装、获取等)一个名为 foobar的可执行文件:

  • github.com/username/go-foobar
  • github.com/username/foobar-tools

并且在包根中有 main.go

90075 次浏览
go build -o <your desired name>

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.

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

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.