The Official Go SDK

Currently, the tools in the official Go SDK are the most used tools to develop Go projects. In Go 101 article series, all examples are compiled and verified with the standard Go compiler.

This article will introduce how to setup the Go development environment and how to run simple Go programs. Some tools of the official Go SDK will also be introduced.

Install Go SDK

Please download the official Go SDK and install it according the instructions at the same page.

The version of an official Go SDK release is consistent with the highest Go language verison the release supports. For example, Go SDK 1.10.x supports all Go versions from 1.0 to Go 1.10.

The path to the bin subfolder in the Go SDK installation root path must be put in the PATH environment variable to execute the tools in the SDK.

(Before Go SDK 1.9, a GOROOT environment variable had to be configured as the root path of the SDK installation. With Go SDK 1.9, this environment variable became optional, and it is recommended to leave it unset.)

Before Go SDK 1.8, a GOPATH environment variable must be set. Since Go SDK 1.8, the default value of this environment variable is the path of the go folder under the home directory of the current user. You may specify GOPATH manually in order to work with one (or more) different location(s). Yes, the GOPATH environment variable may list multiple paths.

It is highly recommended to add the bin subfolder of the very first path specified in the GOPATH environment variable to the PATH environment variable as this allows to run some third-party Go programs due to the go install command putting any resulting executables into this bin subfolder of the first path.

The current official Go SDK (v1.10) recommends to put all your Go packages into the src subfolder of any path specified in the GOPATH environment variable, especially when a Go project depends on some third party packages. However, all examples in Go 101 articles are very simple so that the example files can be put in any folder. Packages will be introduced in the packages and imports article.

Note, the next main Go SDK release (v1.11) may change much so that your Go packages can be put into any folders.

The Simplest Go Program

Let's write a simple exmaple and learn how to run simple Go programs.

The following program is the simplest Go program.
package main

func main() {
}

The words package and func are two keywords. The two main words are two identifiers. Keywords and identifiers are introduced in a coming article.

The first line package main specifies the package name (main here) of the source file.

The second line is a blank line for better readability.

The remaining code declares a function which is also called main. This main function in a main package specifies the entry point of a program. (Note that some other user code might be executed before the main function becomes invoked.)

Run a Go Program

The official Go SDK requires that Go source code file to have the extension .go. Here, we assume the above source code is saved in a file named simplest-go-program.go.

Open a terminal and change the current directory to the directory which contains the above source file, then run

$ go run simplest-go-program.go

Nothing is output? Yes, this program outputs nothing.

If there are some syntax errors in the source code, then there would be some compile errors reported.

Note: If multiple source files are in the main package of a program, then we should run the program with the following command
$ go run *.go

Note, the go run command is not recommended to compile and run large Go projects. It is just a convenient way to run simple Go programs, just like the ones in the Go 101 articles. For large Go projects, use the commands go build or go install instead.

More go Subcommands

The three commands, go run, go build and go install, will only show code syntax errors (if any). They don't (try to) show code warnings (a.k.a, possible code logic mistakes). We can use the go vet command to check and report such warnings.

We can use the go fmt command to format Go source code with a consistent coding style.

We can use the go get command to clone and checkout a remote third-party go package repository. go get requires the corresponding version control tool must be installed. If the command succeeds, the third-party go package will be put in the src folder under the first path specified in the GOPATH environment variable.

We can use the go test command to run tests and benchmarks.

We can use the go doc command to view Go documentations.

The go command run without any arguments shows the supported subcommands.

The Go 101 article series will not explain much more on how to use the tools provided by the official Go SDK. Please read the official documentations for details.

View Go Documentations

We can view all kinds of Go documentations at the official Go website golang.org.

We can also run godoc -http=:9999 to start a local clone of the official website at localhost:9999.

Please note that currently (v1.10), if the GOROOT environment variable is unset, we must specify the goroot flag when running the local godoc server, e. g. godoc -http=:9999 -goroot path/to/go/sdk. This inconvenience may be caused by a bug which may be fixed later.

Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies description flaws and code bugs.