Setting up your Go workspace

Now that you've successfully installed Go on your system, you need to have a properly configured Go workspace before you can proceed further. We will provide a high-level overview on setting up a Go workspace, and if you need further help, you may read the detailed instructions on setting up a Go workspace available at the Go website: https://golang.org/doc/code.html.

Use your favorite text editor to open up the .profile file in your home directory. If you are using Linux, you need to open up the .bashrc file found in your home directory.

We are going to add the following lines to the file to add some very important environment variables:

export GOROOT=/usr/local/go
export GOPATH=/Users/kamesh/go
export GOBIN=${GOPATH}/bin
export PATH=${PATH}:/usr/local/bin:${GOROOT}/bin:${GOBIN}
My username is kamesh, you will have to obviously replace this with your username.

$GOROOT is an environment variable used to specify where the Go distribution is installed on the system.

$GOPATH is an environment variable used to specify the top-level directory containing the source code for all our Go projects. This directory is known as our Go workspace. I have created my workspace in my home directory in the go folder: /Users/kamesh/go.

Let's go ahead and create our Go workspace along with three important directories inside of it:

$ mkdir go
$ mkdir go/src
$ mkdir go/pkg
$ mkdir go/bin

The go/src directory will contain the Go source files. The go/pkg directory will contain the compiled Go packages. Finally, the go/bin directory will contain compiled Go binaries.

$GOBIN is an environment variable used to specify the location where Go should install compiled binaries. When we run the go install command, Go compiles our source code and stores the newly created binary in the directory specified by $GOBIN.

We include two additional entries to the $PATH environment variable—the $GOROOT/bin and $GOBIN directories. This tells our shell environment where to look to find Go-related binaries. Tacking on $GOROOT/bin to the $PATH lets the shell environment know where binaries for the Go distribution are located. Tacking on $GOBIN tells the shell environment where the binaries for the Go programs we create will exist.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.82.4