本文主要记录了阅读 go in action 的笔记
程序执行的顺序
| 12
 3
 4
 
 | func init() {}
 func main() {
 }
 
 | 
初始化一个 go 项目
现在都要使用 go mod,不需要用 GOPATH 之类的
首先要在相应项目下初始化,如
| 1
 | go mod init github.com/goinaction/code/chapter2/sample
 | 
然后
来清理和更新模块的依赖。如果代码中使用了某些包,但这些包没有在 go.mod 中列出,go mod tidy 会自动添加它们
make(map[string]Matcher)
- 如果你只是声明一个 map而不初始化(即var m map[string]int),那么m的值是nil。在这种情况下,尝试向m中添加键值对会导致运行时错误(panic)。
- 使用 make或字面量初始化map可以确保map是非nil的,并且可以安全地添加键值对。
import
| 12
 3
 
 | import (_ "github.com/lib/pq"          // 空白导入(仅初始化)
 )
 
 | 
go vet
错误检查
go fmt
格式化
go mod vendor
自动把依赖的第三方包拷贝到 vendor 目录下
★ 安装指定版本的 go//官方推荐做法
比如我们已经有了 1.23,想要装 1.16
| 1
 | go install golang.org/dl/go1.16@latest
 | 
这样要使用 1.16 的话,把所有的 go version 这样的命令的 go 换成 go1.16 就可以了
Chapter 4 Arrays, slices, and maps
Arrays, slices
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | var arr [5]int
 arr := [3]int{1, 2, 3}
 arr2 := [...]int{1, 2, 3}
 
 
 var slice []int
 slice := []int{1, 2, 3}
 slice := make([]int, 5)
 
 
 
 | 
切片的常用操作和方法:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 
 | slice1 := []int{1, 2, 3}
 slice2 := make([]int, 3, 5)
 var slice3 []int
 
 
 length := len(slice1)
 capacity := cap(slice1)
 
 
 slice1 = append(slice1, 5, 6, 7)
 slice1 = append(slice1, slice2...)
 
 
 slice4 := make([]int, len(slice1))
 copy(slice4, slice1)
 
 
 slice5 := slice1[1:3]
 
 
 for i := 0; i < len(slice1); i++ {
 fmt.Println(slice1[i])
 }
 
 for index, value := range slice1 {
 fmt.Printf("index: %d, value: %d\n", index, value)
 }
 
 
 
 i := 2
 slice1 = append(slice1[:i], slice1[i+1:]...)
 
 
 
 i := 2
 x := 10
 slice1 = append(slice1[:i], append([]int{x}, slice1[i:]...)...)
 
 | 
常用的工具包操作:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | import "sort"
 
 sort.Ints(slice1)
 sort.Float64s(floatSlice)
 sort.Strings(stringSlice)
 
 
 index := sort.SearchInts(slice1, 5)
 
 
 isSorted := sort.IntsAreSorted(slice1)
 
 | 
一些实用技巧:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | slice := make([]int, 0, 1000)
 
 
 slice = slice[:20:20]
 
 
 b := make([]byte, 100)
 b1 := b[:50]
 b2 := make([]byte, len(b1))
 copy(b2, b1)
 
 | 
注意切片容量和长度的概念
map
初始化
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | map1 := make(map[string]int)
 
 
 map2 := map[string]int{
 "apple": 1,
 "banana": 2,
 }
 
 
 var map3 map[string]int
 map3 = make(map[string]int)
 
 | 
基本操作:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | map1["apple"] = 1
 
 
 value := map1["apple"]
 
 
 delete(map1, "apple")
 
 
 value, exists := map1["apple"]
 if exists {
 fmt.Println("键存在,值为:", value)
 } else {
 fmt.Println("键不存在")
 }
 
 
 length := len(map1)
 
 | 
遍历 map:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | for key, value := range map1 {
 fmt.Printf("key: %s, value: %d\n", key, value)
 }
 
 
 for key := range map1 {
 fmt.Printf("key: %s\n", key)
 }
 
 
 for _, value := range map1 {
 fmt.Printf("value: %d\n", value)
 }
 
 | 
Chapter 5 Go’s type system
Reference types
slice, map, channel, interface, and function types
不需要共享引用类型值
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | // 使用切片举例func modifySlice(s []int) {
 s[0] = 100  // 直接修改底层数据
 }
 
 func main() {
 slice := []int{1, 2, 3}
 
 // 不需要这样写:
 // modifySlice(&slice)  // ❌ 不需要传递指针
 
 // 正确的方式:
 modifySlice(slice)     // ✅ 直接传递切片
 
 fmt.Println(slice)     // 输出 [100, 2, 3]
 }
 
 |