Hands-On System Programming with Go
上QQ阅读APP看书,第一时间看更新

Namespace

Now, let's see how Go code is organized. The GOPATH environment variable determines where the code resides. There are three subdirectories inside this:

  • src contains all the source code.
  • pkg contains compiled packages, which are divided into architecture/OS.
  • bin contains the compiled binaries.

The path under the source folder corresponds to the name of the package ($GOPATH/src/my/package/name would be my/package/name).

The go get command makes it possible to fetch and compile packages using it. Go get calls to http://package_name?go-get=1 and if it finds the go-import meta tag, it uses this to fetch the package. The tag should contain the package name, the VCS that was used, and the repository URL, all separated by a space. Let's take a look at an example:

 <meta name="go-import" content="package-name vcs repository-url">

After go get downloads a package, it tries to do the same for the other packages it can't resolve recursively until all the necessary source code is available. 

Each file starts with a package definition, that is, package package_name, that needs to be the same for all the files in a directory. If the package produces a binary, the package is main.