英文:
Go project structure to produce library and cli with the same name in single repository
问题
如何设置项目结构以在单个存储库中生成具有相同名称的库和命令行界面?
假设我的项目名称是project
。我希望它可以以project
的名称进行导入,并且在使用go get
安装时具有名为project
的可执行二进制文件。我的当前设置如下:
host.com/project/
project/
main.go
core/
project.go
然后,当使用以下命令进行安装时:
go get host.com/project/project
它会将project
安装为可执行文件,并将core
作为依赖项引入。在core/project.go
文件中,包的声明如下:
package project
问题在于它被导入为:
import (
"host.com/project/core"
)
并且它将project
作为命名空间导出,而不是core
,这违反了Go的约定。我该如何解决这个问题?
英文:
How to setup project structure to produce library and cli with same name in
single repository?
Suppose my project name is project
. I want to make it importable with name
project
and have executable binary with name project
when installed with
go get
. My setup currently is like this:
host.com/project/
project/
main.go
core/
project.go
Then, when installed with:
go get host.com/project/project
it installs project
as executable which pulls core
as dependency. In
core/project.go
file, the package has this:
package project
The problem is it is imported with:
import (
"host.com/project/core"
)
And it exports project
as name space not core
which violates go's convention.
How can I do this?
答案1
得分: 8
这不是一个约定,你可以按照自己的喜好进行操作,但是通常的做法是在项目根目录下有一个cmd
文件夹,所有的可执行文件都放在其中,每个可执行文件都有自己的文件夹。
所以在你的情况下,一个好的解决方案可能是:
host.com/project/
cmd/
project/
project.go
filea.go
fileb.go
在filea.go
和fileb.go
中,包声明应该是:
package project
在cmd/project/project.go
中,包和导入声明应该是:
package main
import "host.com/project"
当然,如果你的项目更复杂,你可以在项目根目录下创建更多的包,并且它可能有多个命令(多个main
包),例如:
host.com/project/
cmd/
project/
project.go
prjtool/
prjtool.go
packagex/
x.go
packagey/
y.go
filea.go
fileb.go
遵循这种布局的一个例子是非常受欢迎的Go调试器Delve(目前有4.5k个星),可以在https://github.com/derekparker/delve找到。
一个非常复杂的存储库示例是golang.org/x/tools
包,它是多个重要的Go工具的集合,可以在https://github.com/golang/tools/找到。它的cmd
文件夹包含了20多个子文件夹(命令),其中许多工具(main
包)甚至由多个.go
文件组成(但每个文件都使用package main
声明,形成一个可执行文件的单个包)。
还可以查看以下相关问题:https://stackoverflow.com/questions/14867452/what-is-a-sensible-way-to-layout-a-go-project
英文:
This isn't a convention, and you may do however you like, but it's a common practice to have a cmd
folder in the project root, and all the executables go under that, each in is own folder.
So a good solution in your case could be simply:
host.com/project/
cmd/
project/
project.go
filea.go
fileb.go
In filea.go
and fileb.go
the package declaration should be:
package project
In cmd/project/project.go
package and import declaration should be:
package main
import "host.com/project"
Of course if your project is more complex, you may create further packages under the project root, and it may have multiple commands (multiple main
packages), e.g.:
host.com/project/
cmd/
project/
project.go
prjtool/
prjtool.go
packagex/
x.go
packagey/
y.go
filea.go
fileb.go
An example following this layout is the very popular Go Delve debugger (4.5k stars currently), available at https://github.com/derekparker/delve.
A very complex repository example is the golang.org/x/tools
package which is a collection of multiple, essential Go tools, available at https://github.com/golang/tools/. Its cmd
folder contains more than 20 subfolders (commands), many of the tools (main
packages) even consist of multiple .go
files (but each using package main
declaration, forming a single package of the executable).
Also check out the following related question: https://stackoverflow.com/questions/14867452/what-is-a-sensible-way-to-layout-a-go-project
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论