현재 3권의 신간들인 Go Optimizations 101, Go Details & Tips 101Go Generics 101이 출간되어 있습니다. Leanpub 서점에서 번들을 모두 구입하시는 방법이 비용 대비 효율이 가장 좋습니다.

Go에 대한 많은 정보들과 Go 101 책들의 최신 소식을 얻으시려면 Go 101 트위터 계정인 @Go100and1을 팔로잉 해주세요.

The Go Toolchain

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 provided in Go Toolchain.

This article will introduce how to setup the Go development environment and how to use the go command provided in Go Toolchain. Some other tools out of Go Toolchain will also be introduced.

Install Go Toolchain

Please download Go Toolchain and install it by following 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.20.x supports all Go language versions from 1.0 to Go 1.20.

The path to the 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.

The recent Go Toolchain versions support a feature called modules to manage project dependencies. The feature was introduced experimentally in version 1.11 and has been enabled by default since version 1.16.

There is an environment variable, 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.

The Simplest Go Program

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 containing 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.)

Run Go Programs

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.

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 these errors will be reported as compilation errors.

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

More 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 (and should often) 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.

It is highly recommended to let your Go projects support the Go modules feature to ease dependency management. For projects supporting Go modules,

Since Go Toolchain 1.16, we can run 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.

We can use the go help aSubCommand command to view the help message for a specified sub command.

Run the go command without any arguments to show 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.

View Go Package Docs in Browsers

We could use the docs and source view tool Golds (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.

For official Go documentations, please visit go.dev.


Index↡

The Go 101 프로젝트는 Github 에서 호스팅됩니다. 오타, 문법 오류, 부정확한 표현, 설명 결함, 코드 버그, 끊어진 링크와 같은 모든 종류의 실수에 대한 수정 사항을 제출하여 Go 101을 개선을 돕는 것은 언제나 환영합니다.

주기적으로 Go에 대한 깊이 있는 정보를 얻고 싶다면 Go 101의 공식 트위터 계정인 @go100and1을 팔로우하거나 Go 101 슬랙 채널에j가입해주세요.

이 책의 디지털 버전은 아래와 같은 곳을 통해서 구매할 수 있습니다.
Go 101의 저자인 Tapir는 2016년 7월부터 Go 101 시리즈 책들을 집필하고 go101.org 웹사이트를 유지 관리하고 있습니다. 새로운 콘텐츠는 책과 웹사이트에 수시로 추가될 예정입니다. Tapir는 인디 게임 개발자이기도 합니다. Tapir의 게임을 플레이하여 Go 101을 지원할 수도 있습니다. (안드로이드와 아이폰/아이패드용):
  • Color Infection (★★★★★), 140개 이상의 단계로 이루어진 물리 기반의 캐주얼 퍼즐 게임
  • Rectangle Pushers (★★★★★), 2가지 모드와 104개 이상의 단계로 이루어진 캐주얼 퍼즐 게임
  • Let's Play With Particles, 세가지 미니 게임이 있는 캐주얼 액션 게임
페이팔을 통한 개인 기부도 환영합니다.

색인: