Go项目结构以在单个存储库中生成具有相同名称的库和命令行界面(CLI)

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

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.gofileb.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

huangapple
  • 本文由 发表于 2016年11月11日 16:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/40543648.html
匿名

发表评论

匿名网友

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

确定