Go 教程第一课

开始学习Go

2026-08-06

教程下载到本地

go install golang.org/x/website/tour@latest

下载后,在终端输入 tour 启动服务在浏览器看到教程

官方网站

https://go.dev/tour/welcome/1

包、变量和函数。

常见的包

fmt (format)格式化输入输出工具箱

math 数学计算

导入包

1
2
3
4
5
6
7
8
#第一种导入方式
import "fmt"
import "math"
# 第二种导入方式 
import (
"fmt"
"math"

导出名称

  • 首字母大写 = 导出(公开,供外部使用) 比如math.Pi 可以访问
  • 首字母小写 = 未导出(私有,仅限包内使用) 比如math.pi不可以外部访问

方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func关键字 方法名(参数类型 参数) 返回类型{
方法体
}
比如:
func add(x int, y int) int {
	return x + y
}
当参数是同一类型 可以省略前面的类型,只需要保存最后一个类型即可
func add(x, y int) int {
	return x + y
}

变量

var 声明一个变量,如果初始化没有传值,会默认传递默认值(零值机制)

  • 数值类型(如 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">int</font>, <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">float</font>):**<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">0</font>**
  • 布尔类型(<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">bool</font>):**<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">false</font>**
  • 字符串(<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">string</font>):**<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">""</font>**(空字符串)
  • 指针、切片、Map、通道、函数、接口:**<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">nil</font>**

短变量声明

:= 叫做短变量声明 声明:创建新的变量 a 和 b 赋值:把函数的返回结果填进去.。在函数内部可用,函数外部不可用

1
a, b := swap("hello", "world")

基本类型

类别 类型 别名 / 关联 说明(大小/范围/用途)
布尔 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">bool</font> - 布尔值,只能是 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">true</font>
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">false</font>
字符串 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">string</font> - 字符串,底层为字节序列(UTF-8 编码)
有符号整数 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">int</font> - 平台相关,32位系统占4字节,64位系统占8字节
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">int8</font> - 8位有符号整数,范围:-128 ~ 127
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">int16</font> - 16位有符号整数,范围:-32768 ~ 32767
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">int32</font> <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">rune</font> 32位有符号整数,常用于表示Unicode 码点(字符)
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">int64</font> - 64位有符号整数,范围极大
无符号整数 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">uint</font> - 平台相关,同 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">int</font>
位宽(32/64位)
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">uint8</font> <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">byte</font> 8位无符号整数,范围:0 ~ 255,常用于原始字节
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">uint16</font> - 16位无符号整数,范围:0 ~ 65535
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">uint32</font> - 32位无符号整数
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">uint64</font> - 64位无符号整数
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">uintptr</font> - 无符号整型,大小足以存储指针地址(用于底层编程)
浮点数 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">float32</font> - 32位浮点数(IEEE-754 标准),约6位小数精度
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">float64</font> - 64位浮点数(IEEE-754 标准),约15位小数精度(推荐默认使用
复数 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">complex64</font> - 实部和虚部均为 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">float32</font>
的复数
<font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">complex128</font> - 实部和虚部均为 <font style="color:rgb(15, 17, 21);background-color:rgb(235, 238, 242);">float64</font>
的复数

类型转换

<font style="color:rgb(51, 51, 51);background-color:rgb(250, 250, 250);">T(v)</font><font style="color:rgb(51, 51, 51);background-color:rgb(250, 250, 250);">v</font> 转为 <font style="color:rgb(51, 51, 51);background-color:rgb(250, 250, 250);">T</font>类型. 输出 %T 可以看变量是何类型

1
	fmt.Printf("v is of type %T\n", v)

常量

const 关键字来定义,常量不能用短变量声明

常见框架

1. Web 框架 / HTTP 路由(最常用)

框架 特点 推荐场景 GitHub Stars(约)
Gin 高性能、简洁、生态最大 REST API、微服务(首选) 89k+
Echo 干净 API、中间件丰富 REST API、追求代码优雅 33k+
Fiber 类 Express.js、基于 fasthttp 极致性能、Node 转 Go 40k+
Chi 轻量、完全兼容 net/http 想尽量用标准库 23k+
net/http(标准库) 零依赖、官方支持 简单服务、学习基础 -

推荐入门:先学 Gin(资料最多),再尝试 Chi 或直接用标准库。


2. 数据库相关

类型 说明
GORM ORM 最流行的 ORM,功能全、上手快
sqlx SQL 扩展 database/sql 基础上增强,更接近原生 SQL
ent ORM(代码生成) Facebook 开源,类型安全、适合复杂模型
sqlc SQL 代码生成 写 SQL 自动生成类型安全的 Go 代码(近年很火)
go-redis / rueidis Redis 客户端 官方推荐 + 高性能选择

3. 配置、日志、工具类

用途
viper 配置管理(支持 YAML/JSON/环境变量等)
cobra CLI 命令行工具(几乎所有 Go CLI 都用它)
zap / zerolog / logrus 高性能日志(zap 和 zerolog 性能最好)
testify 测试断言 + Mock(几乎所有项目必备)
validator (go-playground) 结构体参数校验
uuid (google/uuid) UUID 生成

4. 微服务 / RPC

框架/包 说明
gRPC + protobuf 官方标准 RPC 方案
go-zero 国内流行的微服务全家桶(含代码生成)
Kratos 字节系微服务框架
go-kit 微服务工具集(偏库而非完整框架)

5. 其他常用实用包

用途
gorilla/websocket WebSocket
cron / robfig/cron 定时任务
excelize Excel 读写
jwt-go / golang-jwt JWT 认证
bcrypt / argon2 密码哈希
lo (samber/lo) 类似 lodash 的工具函数库
resty 优雅的 HTTP 客户端

Licensed under CC BY-NC-SA 4.0
Last updated on Aug 06, 2026 17:36 CST
本站总字数:172.5k 字
载入天数...载入时分秒... ·