100 Mistakes in Golang: Chapter 7

48: Panicking Panicking in Go should be used sparingly. We have seen two prominent cases, one to signal a programmer error and another where our application fails to create a mandatory dependency. 49: Ignoring when to wrap an error In general, the two main use cases for error wrapping are the following: Adding additional context to an error Marking an error as a specific error if err != nil { return fmt.Errorf("bar failed: %w", err) } This code wraps the source error to add additional context without having to create another error type. ...

March 7, 2025 · Last updated on August 1, 2025 · 2 min · KKKZOZ

100 Mistakes in Golang: Chapter 6

42: Not knowing which type of receiver to use In Go, we can attach either a value or a pointer receiver to a method. With a value receiver, Go makes a copy of the value and passes it to the method. Any changes to the object remain local to the method. The original object remains unchanged. On the other hand, with a pointer receiver, Go passes the address of an object to the method. Intrinsically, it remains a copy, but we only copy a pointer, not the object itself (passing by reference doesn’t exist in Go). Any modifications to the receiver are done on the original object. ...

March 4, 2025 · Last updated on August 1, 2025 · 5 min · KKKZOZ

100 Mistakes in Golang: Chapter 5

36: Not understanding the concept of a rune The len built-in function applied on a string doesn’t return the number of characters; it returns the number of bytes. What if we want to get the number of runes in a string, not the number of bytes? How we can do this depends on the encoding. In the previous example, because we assigned a string literal to s, we can use the unicode/utf8 package: ...

March 3, 2025 · Last updated on August 1, 2025 · 3 min · KKKZOZ

100 Mistakes in Golang: Chapter 2

2 Unnecessary nested code 能够提前返回的特殊判定就提前返回 if xxx { // .... return err }else{ // do X } 总是能够写为 if xxx{ return err } // do X Make expected execution flow clear 5 Interface pollution Quote The bigger the interface, the weaker the abstraction. ...

February 20, 2025 · Last updated on August 1, 2025 · 7 min · KKKZOZ

100 Mistakes in Golang: Chapter 3

17 Creating confusion with octal literals In Go, an integer literal starting with 0 is considered an octal integer (base 8). Note the integer literal representations in Golang: Binary - Uses a 0b or 0B prefix (for example, 0b100 is equal to 4 in base 10) Octal - Uses a 0 or 0o prefix (for example, 0o10 is equal 8 in base 10) Hexadecimal - Uses an 0x or 0X prefix (for example, 0xF is equal to 15 in base 10) Imaginary - Uses an i suffix (for example, 3i) 20 Not understanding slice length and capacity In Go, a slice is backed by an array. That means the slice’s data is stored contiguously in an array data structure. A slice also handles the logic of adding an element if the backing array is full or shrinking the backing array if it’s almost empty. ...

February 20, 2025 · Last updated on August 1, 2025 · 12 min · KKKZOZ

100 Mistakes in Golang: Chapter 4

30: Ignoring the fact that elements are copied in range loops type account struct { balance float32 } accounts := []account{ {balance: 100.}, {balance: 200.}, {balance: 300.}, } for _, a := range accounts { a.balance += 1000 } // Output: [{100} {200} {300}] In this example, the range loop does not affect the slice’s content. In Go, everything we assign is a copy: If we assign the result of a function returning a struct, it performs a copy of that struct. If we assign the result of a function returning a pointer, it performs a copy of the memory address (an address is 64 bits long on a 64-bit architecture). Solutions: ...

February 20, 2025 · Last updated on August 1, 2025 · 3 min · KKKZOZ

Handling Golang Errors

Using Error Wrapping An error often “bubbles up” a call chain of multiple functions. In other words, a function receives an error and passes it back to its caller through a return value. The caller might do the same, and so on, until a function up the call chain handles or logs the error. An error can be “wrapped” around another error using fmt.Errorf() and the special formatting verb %w. ...

February 4, 2025 · Last updated on August 3, 2025 · 4 min · KKKZOZ

Golang Struct and Interfaces

这里简单总结一下三种组合方式: interface 中嵌套 interface struct 中嵌套 struct struct 中嵌套 interface interface 中嵌套 interface 这种组合方式体现了接口隔离原则(ISP)和接口组合原则, 通过接口嵌套,我们可以构建更大的接口 // 基础读接口 type Reader interface { Read(p []byte) (n int, err error) } // 基础写接口 type Writer interface { Write(p []byte) (n int, err error) } // 组合接口 type ReadWriter interface { Reader // 嵌套 Reader 接口 Writer // 嵌套 Writer 接口 } struct 中嵌套 struct 这种组合方式体现了组合优于继承12的原则,是 Go 中实现代码复用的重要方式 // 地址信息 type Address struct { Street string City string Country string } // 用户信息 type User struct { Name string Age int Address // 嵌套 Address struct } // 使用示例 func main() { user := User{ Name: "张三", Age: 25, Address: Address{ Street: "中关村大街", City: "北京", Country: "中国", }, } // 可以直接访问嵌套字段 fmt.Println(user.Street) // 输出: 中关村大街 } struct 中嵌套 interface 这种组合方式体现了依赖倒置原则(DIP),常用于策略模式的实现 ...

January 28, 2025 · Last updated on August 1, 2025 · 2 min · KKKZOZ