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:

Example with the following itemsnSet meal

Example with the following items1command

Example with the following items1Command and1package

Example with the following itemsnCommand andnSet meal

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

The 1 package repository located at the following location will be downloaded and installed place it in$GOPATH/src/ We can use in the programimport

What if we type

go get

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

go getWill download it like downloading 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 be compiled asunzip. indeedIs notTo containmain()Features.


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:

        hello                          # command executable
        outyet                         # command executable
                stringutil.a           # package object
            .git/                      # Git repository metadata
            hello.go               # command source
            main.go                # command source
            main_test.go           # test source
            reverse.go             # package source
            reverse_test.go        # test source
            .git/                  # Git repository metadata
            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


Runtime (from anywhere in the system)

go install

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.


More tutorials: