Introduction to Go

last modified December 9, 2020

This is an introduction to the Go programming language. Go first appeared in 2009. Go is developed by Google.


Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. Go is a statically typed, compiled programming language.

Go is similar to C. It is memory safe and has garbage collection. Go has high speed compilation and excellent tooling support. It contains built-in primitives for concurrency.

Go installation

First, we download the binaries from the https://golang.org/doc/install page.

$ ls go1.15.6.linux-amd64.tar.gz 

We have go version 1.15.6.

$ sudo tar -C /usr/local -xzf go1.15.6.linux-amd64.tar.gz

We extract the archive to /usr/local/.

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

We add /usr/local/go/bin to the PATH, so that we can run the Go tool without having to specify the full path.

The Go tool

The go tool provides the standard way to fetch, build, and install Go packages and commands.

$ go version
go version go1.15.6 linux/amd64

With the version option, we get the version of Go.

command description
go build compiles packages and dependencies
go env prints Go environment information
go get adds dependencies to current module and installs them
go install compiles and installs packages and dependencies
go list lists packages and modules
go run compiles and runs a Go program
go version prints Go version

This is a partial list of Go tool commands.

Go first example

We create our first example.

$ mkdir simple
$ cd simple

We create a project directory and relocate there.

$ go mod init zetcode.com/simple

We initiate a Go module.

package main 

import "fmt"

func main() {
    fmt.Println("Go simple example")

This is a simple Go program, which prints a message to the console.

$ go run simple.go 
Go simple example

We can compile and execute the program in one go with go run command.

$ go build
$ ls
go.mod  simple  simple.go
$ ./simple 
Go simple example

We can create an executable program with go build.

$ go install
$ ls ~/go/bin/

With the go install command, we install the binary to the special GOPATH directory, which defaults to $HOME/go on Unix.

In this tutorial, we have introduced the Go language.

List all Go tutorials.