如何使用与Golang包不同的名称构建可执行文件

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

How to build executable with name other than Golang package

问题

如果我的Golang包名是以下之一:github.com/username/go-foobargithub.com/username/foobar-tools,并且在包的根目录中有main.go文件,那么是否可以构建(安装、go 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

and has main.go in the package root?

答案1

得分: 172

你可以使用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 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.

答案2

得分: 5

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

如果你想知道如何使用特定的可执行文件名称或将单个Go文件构建到特定的输出目录中,以下是方法:

# ---------------------
# 通用格式
# ---------------------

# 1. 将输入文件"filename.go"构建为一个可执行文件,
# 名称为"executable_filename",位于当前目录中
go build -o executable_filename filename.go

# 2. 将输入文件"filename.go"构建为一个可执行文件,
# 路径为"output_dir/filename";如果目录"output_dir"不存在,
# 则会自动创建(不要忘记目录名后面的斜杠!)
go build -o output_dir/filename filename.go

# 3. 将输入文件"filename.go"构建为一个可执行文件,
# 路径为"output_dir/whatever";如果目录"output_dir"不存在,
# 则会自动创建
go build -o output_dir/whatever filename.go

# ---------------------
# 示例
# ---------------------

# 从"hello_world.go"创建可执行文件"whatever"
go build -o whatever hello_world.go

# 创建目录"bin"并从"hello_world.go"创建可执行文件"bin/hello_world"
go build -o bin/hello_world hello_world.go

# 创建目录"bin"并从"hello_world.go"创建可执行文件"bin/whatever"
go build -o bin/whatever hello_world.go

# ---------------------
# 帮助
# ---------------------

# 查看`go build`的简短帮助菜单
go build --help

# 查看`go build`的详细帮助菜单
go help build

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

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

usage: go build [-o output] [build flags] [packages]
Run 'go help build' for details.

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

我通过阅读上述文档并进行实验和试错来学习了上述所有内容。

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

英文:

How to specify an output name or path when building in Go (golang)

For anyone landing here wondering how to build single Go files with specific executable names or into specific output directories, here's how:

# ---------------------
# 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

Note that the -o whatever must always come before the input filenames! It is an error to do otherwise.

From go build --help, you can see that -o output must come before build flags and packages:

> usage: go build [-o output] [build flags] [packages]
> Run 'go help build' for details.

For more details, see go help build.

I learned all of the above from the above documentation and my own experimentation and trial and error.

You can try the above commands on my Go examples in my eRCaGuy_hello_world repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/go

huangapple
  • 本文由 发表于 2017年3月10日 05:57:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/42706246.html
匿名

发表评论

匿名网友

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

确定