DevilKing's blog

冷灯看剑,剑上几分功名?炉香无需计苍生,纵一穿烟逝,万丈云埋,孤阳还照古陵

0%

5 things hate about go

原文链接

My personal favorite is that Go does not offer a reentrant lock, i.e. a lock that can be recursively acquired from the same thread or Goroutine (the Go variant of Coroutines or Green Threads). However, without hacks there is no way to build such an implementation by yourself, since threads are not available in Go and Goroutines do not offer an identifier, which would allow you to recognize the same Goroutine you’ve seen before.

1
2
3
4
5
6
7
type task struct {
}

func main() {
task := &task{}
task = &task{}
}

At this point, Go is no longer able to see the distinction between the type and the variable

Type or no type, that is the question!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type handle = int // after go 1.9
type handle int // before go 1.9

func main() {
var var1 int = 1
var var2 handle = 2
types(var1)
types(var2)
}

func types(val interface{}) {
switch v := val.(type) {
case int:
fmt.Println(fmt.Sprintf("I am an int: %d", v))
}
switch v := val.(type) {
case handle:
fmt.Println(fmt.Sprintf("I am an handle: %d", v))
}

Gophers are lazy by nature!

1
2
3
4
5
6
7
8
9
10
11
12
func main() {
functions := make([]func(), 3)
for i := 0; i < 3; i++ {
functions[i] = func() {
fmt.Println(fmt.Sprintf("iterator value: %d", i))
}
}

functions[0]()
functions[1]()
functions[2]()
}

Of nil and nothing

1
2
3
4
5
6
7
func test(v bool) error {
var e *MyError = nil
if v {
return nil
}
return e
}

When e is returned, the *MyError pointer becomes an instance of the interface type error and it’s not nil!