欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

golang 文件目录结构_Go项目的文件系统结构

程序员文章站 2022-06-18 23:19:31
...

golang 文件目录结构

资料夹结构 (Folder structure)

Go projects are composed by packages. We call commands the packages that have the main identifier.

Go项目由程序包组成。 我们将具有main标识符的软件包称为命令

A project can have

一个项目可以有

  • 1 package

    1

  • n packages

    n包裹

  • 1 command

    1命令

  • 1 command and 1 package

    1命令和1程序包

  • n commands and 1 package

    n命令和1软件包

  • 1 command and n packages

    1命令和n软件包

  • n commands and n packages

    n命令和n软件包

Depending on the nature of your project you need to organize the folder structure in a certain way.

根据项目的性质,您需要以某种方式组织文件夹结构。

一些现实世界的例子 (Some real world examples)

Let’s see some real world examples of this in the wild, here below. I took them from Five suggestions for setting up a Go project by Dave Cheney.

让我们在下面看一些真实的例子。 我从戴夫·切尼(Dave Cheney) 提出的关于建立Go项目五个建议中选取了他们。

具有1软件包的项目示例: (Example of a project with 1 package:)

https://github.com/kr/fs

https://github.com/kr/fs

具有n软件包的项目示例 (Example of a project with n packages)

https://github.com/pkg/term

https://github.com/pkg/term

使用1命令的项目示例 (Example of a project with 1 command)

https://github.com/tools/godep

https://github.com/tools/godep

具有1命令和1程序包的项目示例 (Example of a project with 1 command and 1 package)

https://github.com/monochromegane/the_platinum_searcher

https://github.com/monochromegane/the_platinum_searcher

具有n命令和n软件包的项目示例 (Example of a project with n commands and n packages)

https://github.com/golang/tools

https://github.com/golang/tools

项目内文件结构的约定 (Conventions for file structure inside a project)

From those examples we can tell these conventions:

从这些示例中,我们可以得出以下约定:

If you have one command, it can live in a main.go file in the root of the project.

如果您有一个命令,它可以存在于项目根目录下的main.go文件中。

With more than one command, create a cmd/ folder, and under this, create a folder with the name of the command. In each folder, ideally a .go file with the same name will contain the main() function, although it’s not mandatory.

使用多个命令,创建一个cmd/文件夹,并在此文件夹下创建一个带有命令名称的文件夹。 在每个文件夹中,理想情况下,具有相同名称的.go文件将包含main()函数,尽管这不是必需的。

Packages go in their own folder, unless you just have one package.

程序包放在自己的文件夹中,除非您只有一个程序包。

已安装软件包的文件系统位置 (Filesystem location for installed packages)

Any Go project can be installed by using go get. go get will install packages under $GOPATH/src. For example typing

可以使用go get安装任何Go项目。 go get将在$GOPATH/src下安装软件包。 例如输入

go get github.com/kr/fs

will download and install the 1-package repo located at https://github.com/kr/fs, and it will put it under $GOPATH/src/github.com/kr/fs. We can just reference it in our programs using import github.com/kr/fs.

将下载并安装位于https://github.com/kr/fs的1-package存储库,并将其放在$GOPATH/src/github.com/kr/fs 。 我们可以使用import github.com/kr/fs在程序中引用它。

What if we type

如果我们键入怎么办

go get github.com/tools/godep

instead? Remember, this is the one-command repository we listed before.

代替? 请记住,这是我们之前列出的单命令存储库。

go get will download it like it did for github.com/kr/fs, and put it under $GOPATH/src, BUT since it’s a command, it will also compile and create a $GOPATH/bin/godep binary.

go get会像对github.com/kr/fs一样下载它,并将其放在$GOPATH/src ,但由于它是命令,它还会编译并创建$GOPATH/bin/godep二进制文件。

Go commands are compiled to the name of the enclosing folder, in this case the folder was the root one, so it’s godep. In the case of multiple commands, it will take the last subfolder name. E.g. an ideal github.com/flaviocopes/tools/cmd/unzip would be compiled to unzip. It does not take the name of the file containing the main() function.

Go命令被编译为封闭文件夹的名称,在这种情况下,该文件夹是根文件夹,因此它是godep 。 如果有多个命令,它将使用最后一个子文件夹名称。 例如,理想的github.com/flaviocopes/tools/cmd/unzip将被编译为unzip 。 它并不需要包含文件名main()函数。

工作空间 (Workspaces)

Go has a quite unique concept of workspace. A workspace is the folder structure that $GOPATH points to. When you create a new workspace, add the $GOPATH/bin folder to your path as well. For example if you want to set your workspace to ~/go (which by the ways is the default):

Go具有一个非常独特的工作空间概念。 工作区是$GOPATH指向的文件夹结构。 创建新的工作区时,也将$GOPATH/bin文件夹添加到您的路径中。 例如,如果您要将工作空间设置为~/go (顺便说~/go ,这是默认设置):

export GOPATH=~/go
export PATH=~/go/bin:$PATH

Now, any Go code you run will reference this folder. This allows you to create separate workspaces, although as stated in the official docs,

现在,您运行的所有Go代码都将引用此文件夹。 尽管如官方文档中所述 ,这使您可以创建单独的工作区,

Go programmers typically keep all their Go code in a single workspace.

Go程序员通常将所有Go代码保存在一个工作区中。

Note that this differs from other programming environments in which every project has a separate workspace and workspaces are closely tied to version control repositories.

请注意,这与其他编程环境不同,在其他编程环境中,每个项目都有一个单独的工作区,并且这些工作区与版本控制存储库紧密相关。

This is an example of a workspace, listed in the official docs:

这是一个工作区示例,在官方文档中列出:

bin/
        hello                          # command executable
        outyet                         # command executable
    pkg/
        linux_amd64/
            github.com/golang/example/
                stringutil.a           # package object
    src/
        github.com/golang/example/
            .git/                      # Git repository metadata
        hello/
            hello.go               # command source
        outyet/
            main.go                # command source
            main_test.go           # test source
        stringutil/
            reverse.go             # package source
            reverse_test.go        # test source
        golang.org/x/image/
            .git/                  # Git repository metadata
        bmp/
            reader.go              # package source
            writer.go              # package source
        #... (many more repositories and packages omitted) ...

Knowing this, let’s see..

知道了这一点,让我们看看。

命令和软件包的放置位置 (Where to place your commands and packages)

When running commands using go run, you can have your project anywhere you like, but this is an approach useful only for quick testing.

使用go run运行命令时,您可以在任意位置放置项目,但这是仅对快速测试有用的方法。

You should create your programs inside the $GOPATH/src folder, with your unique namespace, e.g.

您应该在$GOPATH/src文件夹中使用唯一的命名空间创建程序,例如

$GOPATH/src/github.com/flaviocopes/hello

When running (from anywhere in the system)

运行时(从系统中的任何位置)

go install github.com/flaviocopes/hello

Go will compile and put the hello binary in $GOPATH/bin, ready to be run from anywhere in the system.

Go将编译并将hello二进制文件放入$GOPATH/bin ,以准备从系统中的任何位置运行。

The same goes for packages, except that when running go install, it will put the compiled package under $GOPATH/pkg.

软件包也是如此, 只是在运行go install ,会将编译后的软件包放在$GOPATH/pkg

参考 (Reference)

翻译自: https://flaviocopes.com/go-filesystem-structure/

golang 文件目录结构

上一篇: Guava使用之Table

下一篇: 缓存