The runtime environment

Download the appropriate package for your operating system and architecture from the Go download page (https://golang.org/dl/):

curl https://golang.org/doc/install?download=go1.10.1.linux-amd64.tar.gz -O /tmp/go1.10.tar.gz
tar -C /usr/local -xzf /tmp/go1.10.tar.gz

The previous commands will download the latest Go package using curl. Then, it will use tar to unpack the package. Next, add the /usr/local/go/bin to the PATH environment variable by adding the following line to your shell's profile script:

export PATH=$PATH:/usr/local/go/bin

If you install Go in a custom directory, rather than /usr/local, you must set the GOROOT environment variable to point to the directory in which it was installed:

export GOROOT=PATH/go
export PATH=$PATH:$GOROOT/bin

Then you have to reload the user profile to apply the changes:

$ source ~/.bash_profile

Now that Go is properly installed and the paths are set for your machine, let's test it out. Create a workspace on which we will build our serverless applications throughout the book:

mkdir -p $HOME/go/src
The Go source code lives in a workspace; by default, it should be $HOME/go. If you'd like to use a different directory, you will need to set the GOPATH environment variable.

To validate that the Go workspace is configured correctly,  you can run the go env command:

If the GOPATH variable is set, you're ready to go. Within the workspace, create a main.go file using vim with the following content:

package main
import "fmt"

func main(){
fmt.Println("Welcome to 'Hands-On serverless Applications with Go'")
}

Compile the file with the following command:

go run main.go

The file will show Welcome to 'Hands-On serverless Applications with Go' if it runs successfully; this shows that Go is compiling files correctly.

Go is a compiled language, and hence you can generate a single binary for your application using the following command:

go build -o app main.go

If you want to build an executable for a specific OS and architecture, you can override the GOOS and GOARCH parameters:

GOOS=linux GOARCH=amd64 go build -o app main.go

Editing Go using the vim text editor is not optimal; therefore, in the next section, I will show you how to use VSCode as a Go editor to enhance your development productivity/experience.