如何使用Golang包以外的名称构建可执行文件

Pet*_*sov 32 build naming-conventions package go

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

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

main.go在包根?

Cra*_*lly 38

您可以使用-o开关指定可执行文件名go build.对于你的例子,它看起来像: 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中包装的东西.

  • 如果您想对特定文件“main.go”执行此操作,并且想将其命名为“myapp”,请像这样运行:“go build -o myapp main.go”。 (4认同)

Gab*_*les 9

在 Go (golang) 中构建时如何指定输出名称或路径

对于任何登陆这里想知道如何构建具有特定可执行名称或特定输出目录的单个 Go 文件的人,方法如下:

# ---------------------
# general formats
# ---------------------

# 1. build input file "filename.go" into an executable
# named "executable_filename" within the directory you are currently `cd`ed
# into
go build -o executable_filename filename.go

# 2. build input file "filename.go" into an executable at
# path "output_dir/filename"; this automatically creates directory "output_dir"
# if it does not already exist (don't forget the trailing slash after the
# directory name!)
go build -o output_dir/ filename.go

# 3. build input file "filename.go" into an executable at
# path "output_dir/whatever"; this automatically creates directory "output_dir"
# if it does not already exist
go build -o output_dir/whatever filename.go

# ---------------------
# examples
# ---------------------

# create executable "whatever" from "hello_world.go"
go build -o whatever hello_world.go

# make directory "bin" and create executable "bin/hello_world"
# from "hello_world.go"
go build -o bin/ hello_world.go

# make directory "bin" and create executable "bin/whatever"
# from "hello_world.go"
go build -o bin/whatever hello_world.go

# ---------------------
# help
# ---------------------

# see the short help menu for `go build`
go build --help

# see the long help menu for `go build`
go help build
Run Code Online (Sandbox Code Playgroud)

请注意,-o whatever必须始终位于输入文件名之前!否则就是错误的。

从 中go build --help,您可以看到它-o output必须出现在构建标志和包之前:

usage: go build [-o output] [build flags] [packages]
Run 'go help build' for details.
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅go help build

我从上述文档以及我自己的实验和反复试验中学到了以上所有内容。

您可以在我的eRCaGuy_hello_world存储库中的 Go 示例上尝试上述命令: https: //github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/go

也可以看看

  1. 用于自动编译 Go 的 Hash bang:我对各种语言的 Hash-bang/shebang 示例的回答,将它们作为可执行文件运行