Go 简介

出身、设计哲学、和 C++/Java/Python 的取舍对比。

Beginner
Key PointsGo 的哲学是「少即是多」:一种循环、一种错误处理、一种并发原语。

出身

Started inside Google in 2007, open-sourced in 2009, 1.0 in 2012. The trigger was the authors' frustration waiting for large C++ builds. One stated goal: compile times short enough to wait for in a single thought.

和别的语言比

AspectGoC++JavaPython
编译秒级分钟级JVM 启动慢解释执行
并发goroutine 内置线程 + 库线程池GIL 限制
部署单个二进制依赖 .so需要 JRE需要解释器
内存GC,低延迟手动GCGC
泛型1.18 起有,较简单模板,复杂擦除式动态

一段代码看清风格

Go
package main

import (
	"errors"
	"fmt"
)

// 返回值带 error,调用方必须处理
func divide(a, b float64) (float64, error) {
	if b == 0 {
		return 0, errors.New("除数不能为零")
	}
	return a / b, nil
}

func main() {
	r, err := divide(10, 0)
	if err != nil {
		fmt.Println("出错:", err)
		return
	}
	fmt.Println("结果:", r)
}
Output
出错: 除数不能为零

Two things to notice: error is an ordinary return value, not an exception; and if err != nil will be the three lines you write most. That is Go's entire error model.

谁在用

  • Docker / Kubernetes / containerd —— 云原生底座
  • Prometheus / Grafana / etcd —— 可观测性与协调服务
  • Terraform / Vault —— 基础设施即代码
  • 字节、腾讯、B站的网关与中间件 —— 高并发场景
Go is not a do-everything language. It is clearly weaker than Python, C++ or Rust for GUIs, ML training and scientific computing. Choose it for server-side concurrency and deployment cost, not versatility.

Friends & Partners