已发布·持续改进
Go 指南 · 1/6
本章目前仅提供英文版。
Go was designed to be boring in the best sense: a small language, a fast compiler, and concurrency built in from day one. This chapter walks you from a fresh machine to a compiled binary. You will install the toolchain, create a module, run and build a program, and wire up your editor so it catches mistakes as you type.
Grab an installer for your platform from go.dev/dl. On Windows and macOS, run the installer and you are done. On Linux, the usual approach is to unpack the archive into /usr/local and add /usr/local/go/bin to your PATH. Package managers such as Homebrew or winget work too, though Linux distribution repositories sometimes lag well behind the current release, so check what you actually got.
# Linux: replace any previous install and put go on the PATH
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go*.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
# Confirm everything is wired up
go version
go env GOPATH GOROOTIf go version prints a version string, the install worked. go env shows the settings the toolchain uses. GOROOT is where Go itself lives. GOPATH holds the module download cache and, under GOPATH/bin, any binaries you install with go install. Adding that bin directory to your PATH saves a lot of typing later.
Go organizes code into modules: a directory containing a go.mod file, plus every package beneath it. Start each new project with go mod init and a module path. By convention, the path is where the code will eventually live, such as a repository URL, but any unique name works while you experiment.
mkdir hello && cd hello
go mod init example.com/helloNow add main.go. Anything you want to compile into an executable must be in package main and must define a main function, which is where execution begins.
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}go run compiles and runs in one step, dropping the binary in a temporary directory. It is ideal for quick iterations. go build leaves a real executable behind in the current directory.
go run . # compile the current package and run it
go build # produces ./hello (hello.exe on Windows)
go build -o bin/app # choose the output path
./hello
# Cross-compile for another platform: just set two variables
GOOS=linux GOARCH=arm64 go build -o hello-linux-arm64By default a Go binary is a single self-contained file, which makes deployment refreshingly simple: copy it to the server and run it. Cross-compiling needs no extra toolchain; GOOS and GOARCH are all it takes.
| Command | What it does |
|---|---|
go run . | Compile and run immediately |
go build | Build an executable or check that a package compiles |
go test ./... | Run tests in every package |
go fmt ./... | Rewrite code into the standard format |
go vet ./... | Static checks for common bugs |
go mod tidy | Sync go.mod with what the code imports |
go doc fmt.Println | Read documentation in the terminal |
The ./... pattern means "this directory and everything below it." Because gofmt defines the one true layout, Go teams simply do not argue about brace placement or indentation. Go source uses tabs for indentation, and the formatter takes care of the rest.
A tiny program needs nothing more than go.mod and main.go. As a codebase grows, a layout like the one below is common. None of it is mandatory, with one exception: the toolchain gives the internal directory special meaning.
hello/
go.mod # module path and dependency requirements
go.sum # checksums for those dependencies
main.go # package main
cmd/
server/main.go # one directory per extra binary
internal/
store/store.go # importable only from inside this module
greet/
greet.go # a package other modules may importEvery directory is exactly one package, and all .go files in it must declare the same package name. Packages under internal can only be imported by code rooted at the parent of that internal directory, which makes it the natural home for implementation details you do not want to support publicly.
gopls (pronounced "go please") is the official language server maintained by the Go team. It powers completion, go-to-definition, rename, diagnostics, and automatic import management. In VS Code, install the Go extension and accept its prompt to install the tools. GoLand ships its own analysis engine. Neovim, Emacs, Helix, and other LSP-capable editors can all talk to gopls directly.
go install golang.org/x/tools/gopls@latest
gopls versionConfigure your editor to run gofmt or goimports on save. This matters more in Go than elsewhere: unused imports and unused local variables are compile errors, not warnings, so having imports added and removed automatically keeps you in flow.
go version.go mod init <module path>; executables live in package main and start at main.go run for quick feedback, go build for binaries, and GOOS/GOARCH for cross-compiling.internal keeps code private to your module.gopls into your editor and format on save.
0 条评论
登录 · 登录后即可发表评论。
来发表第一条评论吧。