go command provided in Go Toolchain. Some other tools out of Go Toolchain will also be introduced.
bin subfolder in Go Toolchain installation root path must be put in the PATH environment variable to execute the tools (through the go command) 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.
GOPATH, we should be aware of, though we don't need to care too much about it. It is defaulted to the path to the go folder under the home directory of the current user. The GOPATH environment variable may list multiple paths if it is specified manually. Later, when the GOPATH folder is mentioned, it means the folder at the first path in the GOPATH environment variable.
pkg subfolder under the GOPATH folder is used to store cached versions of Go modules (a Go module is a collection of Go packages) depended by local Go projects.
GOBIN environment variable which controls where the Go program binary files generated by the go install subcommand will be stored. The value of the GOBIN environment variable is defaulted to the path to bin subfolder under the GOPATH folder. The GOBIN path should be set in the PATH environment variable if you would run the generated Go program binary files without specifying their full paths.
package main
func main() {
}
package and func are two keywords. The two main words are two identifiers. Keywords and identifiers are introduced in a coming article.
package main specifies the package name (main here) of the containing source file.
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. Here, we assume the above source code is saved in a file named simplest-go-program.go.
$ go run simplest-go-program.go
main package of a program, then we should run the program with the following command
$ go run .
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 then run executable binary files instead.
go.mod file located in the root folder of that project. The go.mod file can be generated by the go mod init subcommand (see below).
_ or . are ignored by Go Toolchain.
go Subcommands
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.
go fmt command to format Go source code with a consistent coding style.
go test command to run tests and benchmarks.
go doc command to view Go documentation in terminal windows.
go mod init example.com/myproject command is used to generate a go.mod file in the current directory, which will be viewed as the root directory of a module called example.com/myproject. The go.mod file will be used to record module dependencies. We can edit the go.mod file manually or let the go subcommands to update it.
go mod tidy command is used to add missing module dependencies into and remove unused module dependencies from the go.mod file by analyzing all the source code of the current project.
go get command is used to add/upgrade/downgrade/remove a single dependency. This command is used less frequently than the go mod tidy command.
go install example.com/program@latest to install the latest version of a third-party Go program (into the GOBIN folder). Before Go Toolchain 1.16, the corresponding command is go get -u example.com/program, which has been deprecated now.
go help aSubCommand command to view the help message for a specified sub command.
go command without any arguments to show the supported subcommands.
go101.org/golds) to locally view the docs of a specified Go project. This is not an official tool. It is developed by me (Tapir Liu). Golds supports rich code reading experiences (such as listing type implementation relations). After installing Golds, we can run golds std ./... to view the docs of standard packages and packages in the current folder.
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 @zigo_101.
reflect standard package.sync standard package.sync/atomic standard package.