env
GOPATH
GOPATH
是 Go 工作区的根目录,通常包含三个子目录:src
、pkg
和 bin
可通过 go env GOPATH 查看Go 工作区的根目录。
GOPATH
中的bin
目录通常包含由go install
安装的可执行文件
Syntax
sclice
sclice selects a half-open range which includes the first element, but excludes the last one.
syntax
slice[low:high]
“take from 1 to the end” with
numbers[1:]
.A slice does not store any data, it just describes a section of an underlying array. Changing the elements of a slice modifies the corresponding elements of its underlying array. Other slices that share the same underlying array will see those changes.
The default is zero for the low bound and the length of the slice for the high bound.
1
2
3
4
5
6var a [10]int
# is equal below
a[0:10]
a[:10]
a[0:]
a[:]
Go’s while
C’s while
is spelled for
in Go.
1 | for sum < 1000 { |
error
- errors are values, so we can refactor it out into a variable and have a single source of truth for it.
1 | var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds") |
pointer
A pointer holds the memory address of a value.
The
&
operator generates a pointer to its operand.The
*
operator denotes the pointer’s underlying value.
1 | i := 42 |
when you call a function or a method the arguments are copied.
1 | //When calling func (w Wallet) Deposit(amount int) the w is a copy of whatever we called the method from. |
- 不可寻址的情况:
- 不可变的值不可寻址。常量、基本类型的值字面量、字符串变量的值、函数以及方法的字面量都是如此。其实这样规定也有安全性方面的考虑。
- 绝大多数被视为临时结果的值都是不可寻址的。算术操作的结果值属于临时结果,针对值字面量的表达式结果值也属于临时结果。但有一个例外,对切片字面量的索引结果值虽然也属于临时结果,但却是可寻址的。
- 若拿到某值的指针可能会破坏程序的一致性,那么就是不安全的,该值就不可寻址。由于字典的内部机制,对字典的索引结果值的取址操作都是不安全的。另外,获取由字面量或标识符代表的函数或方法的地址显然也是不安全的。
assert
1 | value, ok := interface{}(container).(map[int]string) |
data type
- 引用类型:切片、字典、通道、函数
- 值类型:数组、基础数据类型以及结构体类型
concurrency
Do not communicate by sharing memory; instead, share memory by communicating.
backstone
PMG stands for P (logical processors), M (machine threads), and G (goroutines). The key point is that each logical processor (P) can only have one machine thread (M) running on it at any time. And for a goroutine (G) to run, it needs to be attached to a thread (M).
sync
Mutex
allows us to add locks to our datamutual exclusion,简称 mutex
WaitGroup
is a means of waiting for goroutines to finish jobsRWMutex
读写锁
channel
they provide a way to send data from one goroutine to another, ensuring smooth communication and synchronization in concurrent programs.
best practise
directory
1 | project-root/ |
Paraphrasing
Use channels when passing ownership of data
Use mutexes for managing state
名称的首字母为大写的程序实体才可以被当前包外的代码引用,否则它就只能被当前包内的其他代码引用。
一对不包裹任何东西的花括号,除了可以代表空的代码块之外,还可以用于表示不包含任何内容的数据结构(或者说数据类型)
函数类型属于引用类型,它的零值是
nil
。结构体类型中的一个字段看作是它的一个属性或者一项数据,再把隶属于它的一个方法看作是附加在其中数据之上的一个能力或者一项操作。将属性及其能力(或者说数据及其操作)封装在一起,是面向对象编程(object-oriented programming)的一个主要原则。
test
go test -v
go test -cover
1 | 测试指定目录下的某个方法 |
tool
errcheck
1
errcheck .
A vs B
- Golang’s goroutine VS Python’s coroutine
Aspect | Go (Goroutines) | Python (Coroutines) |
---|---|---|
Concurrency Model | Preemptive, managed by Go runtime (M:N threading) | Cooperative, event-loop-based (async/await) |
Execution Model | True concurrency, can run in parallel on multiple cores | Single-threaded, asynchronous (mostly I/O-bound) |
Syntax | go func() |
async def and await |
Task Management | Automatic, via Go runtime | Manual, via event loop (e.g., asyncio ) |
Performance | Efficient, ideal for both I/O-bound and CPU-bound tasks | Efficient for I/O-bound, not CPU-bound tasks |
Error Handling | Requires manual management via channels or sync | Propagates through await /event loop |