How to configure binary name in Go without changing path

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

How to configure binary name in Go without changing path

问题

我正在开发一个使用Go语言的小工具。最近,我注意到这个工具需要从一个shell脚本中调用,因为它使用了shell函数。

假设我的工具叫做atool。所以,go build会生成一个名为atool的可执行文件,而我的工具的Go结构体路径是github.com/myaccount/atool。现在,我想用go build构建一个名为atool-cli的可执行文件,并从shell脚本atool中调用它。我该如何实现这个目标?

我脑海中唯一的想法是将Go结构体路径改为github.com/myaccount/atool-cli。但我不想这样做,因为已经宣布了这个路径,而且这个路径看起来有点奇怪。

英文:

I'm developing a small tool with Go. And recently, I noticed that the tool needs to be invoked from a shell script, because it's using shell function.

Assume my tool is called atool. So, go build generates a binary atool, and my tool has a Go structure as github.com/myaccount/atool. Now, i want to build atool-cli binary with go build, and invoke it from shell script atool. How can I achieve this?

The only way coming in my mind is change go structure as github.com/myaccuont/atool-cli. But I don't want to do this because the already announced, and also, the path seems a bit funny name.

答案1

得分: 22

只是为了让我的评论“正式”一些:

go build -o atool-cli github.com/you/atool
英文:

Just to make my comment "official":

go build -o atool-cli github.com/you/atool

答案2

得分: 9

一种将包结构化为库并提供主要包的方式是将它们的主要入口点放在子目录中。

你可以在 github.com/myaccount/atool/atool-cli 中有一个 main 包,它导入 github.com/myaccount/atool 并实现 func main()。一些具有多个命令的包甚至在 /cmd/ 目录下有多个可以构建的命令行工具(例如 camlistore)。

英文:

One way packages structure themselves as a library, and provide main packages is to put their main entrypoints in subdirectories.

You can have a main package in github.com/myaccount/atool/atool-cli, which imports github.com/myaccount/atool and implements func main(). Some packages with multiple commands even have a /cmd/ directory with multiple cli tools that can be built (see camlistore as an example)

huangapple
  • 本文由 发表于 2014年8月2日 16:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/25093129.html
匿名

发表评论

匿名网友

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

确定