Go(常称 Golang)由 Google 于 2009 年开源,目标很明确:在保持接近 C 的执行效率的同时, 让大规模软件工程变得更简单——编译快、依赖清晰、并发原语一等公民。
为什么选择 Go
- 语法克制:关键字少,读代码成本低,团队协作时分歧更少。
- 工具链完整:格式化(
gofmt)、测试、模块、交叉编译开箱即用。 - 并发友好:goroutine 与 channel 让「轻量并发」成为日常写法。
- 部署简单:静态链接的单一二进制,容器与服务器上都很省心。
Hello, World
package main
import "fmt"
func main() {
fmt.Println("你好,万象境")
}
保存为 hello.go 后执行 go run hello.go 即可看到输出。
并发一瞥
用 go 关键字启动 goroutine,再通过 channel 传递结果,是 Go 最常见的并发模式:
package main
import (
"fmt"
"time"
)
func worker(id int, done chan<- string) {
time.Sleep(100 * time.Millisecond)
done <- fmt.Sprintf("worker %d 完成", id)
}
func main() {
done := make(chan string, 2)
go worker(1, done)
go worker(2, done)
fmt.Println(<-done)
fmt.Println(<-done)
}
适合什么场景
API / 微服务、CLI、网络代理、可观测性组件、云基础设施工具……凡是重视「工程效率 + 可运维性」的后端场景, Go 往往是稳妥选择。它不追求语言特性的极致炫技,而是把复杂度留在业务本身。
若你刚起步,官方文档 go.dev/doc 与 A Tour of Go 是最好的入口;写完几个小服务后,你会很快感受到这门语言的「工程气质」。