Currently, the tools in the official Go Toolchain (called Go Toolchain later) 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 Go Toolchain will also be introduced.
Please download Go Toolchain and install it according to the instructions shown in the download page.
The version of a Go Toolchain release is consistent with the highest Go language version the release supports. For example, Go Toolchain 1.15.x supports all Go language versions from 1.0 to Go 1.15.
The path to the bin
subfolder in Go Toolchain installation root path
must be put in the PATH
environment variable to execute the tools
(mainly the go
subcommands) provided in Go Toolchain without inputting their full paths.
If your Go Toolchain is installed through an installer or with a package manager, the path to
the bin
subfolder may have been already set in the PATH
environment variable automatically for you.
Earlier Go Toolchain versions might require GOROOT
and
GOPATH
environment variables to be set.
Recent Go Toolchain versions have no such requirements.
The default value of the GOPATH
environment variable is
the path to the go
folder under the home directory of the current user.
GOPATH
environment variable may list multiple paths.
There is a GOBIN
environment variable which controls
where the binary files generated by some go
subcommands,
such as the go install
subcommand (see below), will be stored.
If the environment variable is not set, the go
command will
use the path to bin
subfolder in the first path specified in the
GOPATH
environment variable to store the generated binary files.
The path to the folder for storing the binary files should be set in the PATH
environment variable to run the binary files without specifying their full paths.
Before Go Toolchain 1.11, it is recommended to put all custom Go packages into the
src
subfolder of any path specified in the GOPATH
environment variable,
in particular when a Go project depends on some third party packages.
Packages will be introduced in packages and imports later.
In Go Toolchain 1.11, an experimental feature, Go modules, is supported. The Go modules feature lets us put our Go projects freely in any folder. We can get more module releated information from this wiki page.
Note, since Go Toolchain 1.13,
the Go modules feature becomes as the preferred mode (to the old GOPATH
mode).
The necessity and meaningfulness of the GOPATH
environment variable
will be weakened much, even be abolished eventually.
On the other hand, the importance of the GOBIN
environment variable will be promoted,
for there is still a need to store the binary files produced by some go
subcommands.
It is planned that the Go modules feature will be enabled by default since the next version (1.16).
Let's write a simple example 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 gets invoked.)
Go Toolchain 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
.
$ 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 these errors will be reported as compilation errors.
If multiple source files are in themain
package of a program,
then we should run the program with the following command
$ go run .
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, like the ones in the Go 101 articles.
For large Go projects, please use the commands go build
or go install
to build and create executable binary files instead.
go
Subcommands
The three commands, go run
, go build
and go install
,
only output code syntax errors (if any).
They don't (try to) output 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 test
command to run tests and benchmarks.
We can use the go doc
command to view Go documentation in terminal windows.
Since Go Toolchain 1.11, we can use the go mod
command to manage module dependencies.
We can use the go help aSubCommand
command to view the help message for a specified sub command.
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 in Go Toolchain. Please read the official documentation for details.
We can view all kinds of Go documentation on the official Go website golang.org and package docs on go.dev.
We can also install the godoc program
(golang.org/x/tools/cmd/godoc
),
then run godoc -http=:9999
to start a local clone of Go
official website at localhost:9999.
The godoc tool can also be used to view docs of Go projects locally.
Another docs view tool is Golds
(go101.org/golds
), which is developed by me (Tapir Liu).
Golds supports rich code reading experience and lists type implementation relations.
After installing Golds, we can run golds std
to read the docs of the standard packages.
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account: @go100and1.