golang 文件目录结构

资料夹结构 (Folder structure)

main
main

A project can have

一个项目可以有

11nn111111n1n11n1nnnnn

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项目的五个建议中选取了他们。

11
nn
11
1111
nnnn

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

From those examples we can tell these conventions:

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

main.go
main.go
cmd/.gomain()
cmd/.gomain()

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

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

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

go getgo get$GOPATH/src
go getgo get$GOPATH/src
$GOPATH/src/github.com/kr/fsimport github.com/kr/fs
$GOPATH/src/github.com/kr/fsimport github.com/kr/fs

What if we type

如果我们键入怎么办

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

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

go getgithub.com/kr/fs$GOPATH/src$GOPATH/bin/godep
go getgithub.com/kr/fs$GOPATH/src$GOPATH/bin/godep
godepgithub.com/flaviocopes/tools/cmd/unzipunzipmain()
godepgithub.com/flaviocopes/tools/cmd/unzipunzipmain()

工作空间 (Workspaces)

$GOPATH$GOPATH/bin~/go
$GOPATH$GOPATH/bin~/go~/go

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:

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

Knowing this, let’s see..

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

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

go run
go run
$GOPATH/src
$GOPATH/src

When running (from anywhere in the system)

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

hello$GOPATH/bin
hello$GOPATH/bin
go install$GOPATH/pkg
go install$GOPATH/pkg

参考 (Reference)

golang 文件目录结构