File system structure of Go project

Folder structure

The Go project consists of the following peopleSet meal. We call itcommandhavemainIdentifier.

A project can have

  • 1package
  • nSet meal
  • 1command
  • 1Command and1package
  • nCommand and1package
  • 1Command andnSet meal
  • nCommand andnSet meal

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

Some real world examples

Let us look at some real examples below. I took them fromFive suggestions for setting up a Go projectWritten by Dave Cheney.

Example with the following items1package:

https://github.com/kr/fs

Example with the following itemsnSet meal

https://github.com/pkg/term

Example with the following items1command

https://github.com/tools/godep

Example with the following items1Command and1package

https://github.com/monochromegane/the_platinum_searcher

Example with the following itemsnCommand andnSet meal

https://github.com/golang/tools

Agreement on file structure within the project

From these examples, we can draw the following conventions:

If you have a command, it can exist inmain.goThe files are placed in the root directory of the project.

Use multiple commands to create onecmd/Folder, and then create a folder with the command name under this folder. In each folder, ideally.goA file with the same name will containmain()Function, although not mandatory.

The package is placed in its own folder, unless you only have one package.

File system location of the installed package

Any Go project can be installed using the following commandgo get.go getThe package will be installed in the following location$GOPATH/src. For example, enter

go get github.com/kr/fs

The 1 package repository located at the following location will be downloaded and installedhttps://github.com/kr/fsAnd place it in$GOPATH/src/github.com/kr/fs. We can use in the programimport github.com/kr/fs.

What if we type

go get github.com/tools/godep

instead? Remember, this is the single command repository we listed earlier.

go getWill download it like downloading itgithub.com/kr/fsAnd place it in$GOPATH/src, But since it is a command, it will also compile and create a$GOPATH/bin/godepBinary.

The Go command is compiled to the name of the folder in which it is located. In this case, the folder is the root folder, so it isgodep. In the case of using multiple commands,Last subfolderName. Such as idealgithub.com/flaviocopes/tools/cmd/unzipWill be compiled asunzip. indeedIs notTo containmain()Features.

Workspace

Go has a very unique conceptWork area. The workspace is a folder structure,$GOPATHPointing. When creating a new workspace, add$GOPATH/binFolder and your path. For example, if you want to set the workspace to~/go(This is the case by default):

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

Now all the Go code you run will reference this folder. Despite the above, this allows you to create a separate workspaceIn the official document,

Go programmers usually save all Go code in a workspace.

Please note that this is different from other programming environments, where each project has a separate workspace, and these workspaces are closely related to the version control repository.

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

    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 us see.

Where to place commands and packages

When running the command withgo run, You can place items wherever you like, but this is a method that is only useful for quick testing.

You should be at$GOPATH/srcFolder with your unique namespace, for example

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

Runtime (from anywhere in the system)

go install github.com/flaviocopes/hello

Go will compile and puthelloBinary input$GOPATH/bin, Can be run from any location in the system.

The same goes for packagesapart fromWhile runninggo install, It puts the compiled package in$GOPATH/pkg.

reference


More tutorials: