1.2 $GOPATH and workspace

$GOPATH

Go takes a unique approach to manage the code files with the introduction of a $GOPATH directory which contains all the go code on the machine. Note that this is different from the $GOROOT environment variable which states where go is installed on the machine. We have to define the $GOPATH variable before using the language, in *nix systems there is a file called .profile we need to append the below export statement to the file. The concept behind gopath is a novel one, where we can link to any go code at any instant of time without ambiguity.

Starting from go 1.8, the GOPATH environment variable now has a default value if it not set: it defaults to $HOME/go on Unix and %USERPROFILE%/go on Windows.

On Unix-like systems, the variable should be used like this:

  1. export GOPATH=${HOME}/mygo

In Windows, you need to create a new environment variable called GOPATH, then set its value to c:\mygo( This value depends on where your workspace is located )

It’s OK to have more than one path (workspace) in $GOPATH, but remember that you have to use :(; in Windows) to separate them. At this point, go get will save the content to your first path in $GOPATH. It is highly recommended to not have multiples versions, the worst case is to create a folder by the name of your project right inside $GOPATH, it breaks everything that the creators were wishing to change in programming with the creation of go language because when you create a folder inside $GOPATH you will reference your packages as directly as , and this breaks all the applications which will import your package because the go get won’t find your package. Please follow conventions, there is a reason conventions are created.

In $GOPATH, you must have three folders as follows:

  • src for source files whose suffix is .go, .c, .g, .s.
  • pkg for compiled files whose suffix is .a.
  • bin for executable files

In this book, I use mygo as my only path in $GOPATH.

Package directory

Create package source files and folders like $GOPATH/src/mymath/sqrt.go (mymath is the package name) ( Author uses mymath as his package name, and the same name for the folder that contains the package source files)

Every time you create a package, you should create a new folder in the src directory, with the notable exception of main, for which main folder creation is optional. Folder names are usually the same as the package that you are going to use. You can have multi-level directories if you want to. For example, if you create the directory $GOPATH/src/github.com/astaxie/beedb, then the package path would be github.com/astaxie/beedb. The package name will be the last directory in your path, which is beedb in this case.

Execute following commands. ( Now author goes back to talk examples )

  1. cd $GOPATH/src
  2. mkdir mymath

Create a new file called sqrt.go, type the following content to your file.

  1. // Source code of $GOPATH/src/mymath/sqrt.go
  2. package mymath
  3. func Sqrt(x float64) float64 {
  4. z := 0.0
  5. for i := 0; i < 1000; i++ {
  6. z -= (z*z - x) / (2 * x)
  7. }
  8. return z
  9. }

Now my package directory has been created and its code has been written. I recommend that you use the same name for your packages as their corresponding directories, and that the directories contain all of the package source files.

Compile packages

We’ve already created our package above, but how do we compile it for practical purposes? There are two ways to do this.

  1. Switch your work path to the directory of your package, then execute the go install command.
  2. Execute the above command except with a file name, like go install mymath.

After compiling, we can open the following folder.

  1. cd $GOPATH/pkg/${GOOS}_${GOARCH}
  2. // you can see the file was generated
  3. mymath.a

The file whose suffix is .a is the binary file of our package. How do we use it?

Obviously, we need to create a new application to use it.

Create a new application package called mathapp.

  1. cd $GOPATH/src
  2. mkdir mathapp
  3. cd mathapp
  4. vim main.go

Write the following content to main.go.

  1. //$GOPATH/src/mathapp/main.go source code.
  2. package main
  3. import (
  4. "mymath"
  5. "fmt"
  6. )
  7. func main() {
  8. fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
  9. }

To compile this application, you need to switch to the application directory, which in this case is $GOPATH/src/mathapp, then execute the go install command. Now you should see an executable file called mathapp was generated in the directory $GOPATH/bin/. To run this program, use the ./mathapp command. You should see the following content in your terminal.

  1. Hello world. Sqrt(2) = 1.414213562373095

Install remote packages

Go has a tool for installing remote packages, which is a command called go get. It supports most open source communities, including GitHub, Google Code, BitBucket, and Launchpad.

  1. go get github.com/astaxie/beedb

You can use go get -u … to update your remote packages and it will automatically install all the dependent packages as well.

This tool will use different version control tools for different open source platforms. For example, git for GitHub and hg for Google Code. Therefore, you have to install these version control tools before you use go get.

After executing the above commands, the directory structure should look like following.

  1. $GOPATH
  2. src
  3. |-github.com
  4. |-astaxie
  5. |-beedb
  6. pkg
  7. |--${GOOS}_${GOARCH}
  8. |-github.com
  9. |-astaxie
  10. |-beedb.a

Actually, go get clones source code to the $GOPATH/src of the local file system, then executes go install.

You can use remote packages in the same way that we use local packages.

  1. import "github.com/astaxie/beedb"

Directory complete structure

If you’ve followed all of the above steps, your directory structure should now look like the following.

  1. bin/
  2. mathapp
  3. pkg/
  4. ${GOOS}_${GOARCH}, such as darwin_amd64, linux_amd64
  5. mymath.a
  6. github.com/
  7. astaxie/
  8. beedb.a
  9. src/
  10. mathapp
  11. main.go
  12. mymath/
  13. sqrt.go
  14. github.com/
  15. astaxie/
  16. beedb/
  17. beedb.go
  18. util.go

Now you are able to see the directory structure clearly: bin contains executable files, pkg contains compiled files and src contains package source files.

(The format of environment variables in Windows is %GOPATH%, however this book mainly follows the Unix-style, so Windows users need to replace these yourself.)