Go Overview & Environment Setup
Why Go was created, its design philosophy, installing the Go toolchain, understanding GOPATH and module mode, and writing your first Go program.
Why Go Exists
Google created Go in 2009 to solve three pain points: slow C++/Java compiles, unwieldy dependency management, and the complexity of concurrent programming. Go compiles to a single static binary, has a tiny keyword set (25), garbage collection, and goroutines that make concurrency a language-level feature rather than a library afterthought.
- Cloud-native pedigree: Docker, Kubernetes, Terraform, etcd are all written in Go.
- Fast compilation: large projects build in seconds.
- Simple concurrency: goroutines and channels are built into the language.
Installing the Toolchain
# 下载安装包(go.dev/dl)后验证
go version # go1.23+
# Linux 包管理器
sudo apt install golang-go
# 环境变量(默认即可,需要自定义时再改)
go env GOPATH
go env GOMODCACHE
Modern Go is module-based: every project has a go.mod and lives anywhere on disk. GOPATH mode is only for very old code.
First Program
mkdir hello && cd hello
go mod init example.com/hello # 生成 go.mod
go get github.com/gin-gonic/gin # 添加依赖
go build # 编译为当前平台二进制
go run . # 直接运行
package main
import "fmt"
func main() {
fmt.Println("Hello, BackendWiki!")
}
- main package + func main is the entry point.
- Unreferenced imports are a compile error — keep imports tidy.
- gofmt / gofmt -w formats code; there is one canonical style, no formatting debates.
Go 为什么诞生
Google 于 2009 年创造 Go,直指三大痛点:C++/Java 编译慢、依赖管理臃肿、并发编程复杂。Go 编译出单一静态二进制,关键字只有 25 个,自带垃圾回收,goroutine 把并发做成了语言级特性而非库的补丁。
- 云原生血统:Docker、Kubernetes、Terraform、etcd 全部用 Go 编写。
- 编译飞快:大型项目秒级构建。
- 简洁并发:goroutine 与 channel 是语言原生支持。
安装工具链
# 下载安装包(go.dev/dl)后验证
go version # go1.23+
# Linux 包管理器
sudo apt install golang-go
# 环境变量(默认即可,需要自定义时再改)
go env GOPATH
go env GOMODCACHE
现代 Go 是 module 模式:每个项目有 go.mod,可以放在磁盘任意位置。GOPATH 模式只用于非常老的代码。
第一个程序
mkdir hello && cd hello
go mod init example.com/hello # 生成 go.mod
go get github.com/gin-gonic/gin # 添加依赖
go build # 编译为当前平台二进制
go run . # 直接运行
package main
import "fmt"
func main() {
fmt.Println("Hello, BackendWiki!")
}
- main 包 + func main 是程序入口。
- 未使用的 import 会编译报错,保持 import 整洁。
- gofmt / gofmt -w 统一格式化,全社区只有一种代码风格。