Open jasperzhong opened 2 years ago
重新过了一遍https://tour.golang.org/list 我重新爱上了golang...
最exciting部分当然是concurrency. goroutine其实和thread差不多,但语法简单多了,直接go func(...)
. channel看上去是一个blocking queue,但是go提供channel selection功能 (select
),而且语法如此简单,用C++方便实现么? I have no idea.
func fibonacci(c, quit chan int) {
x := 0
for {
select {
case c <- x:
fmt.Println(x)
x += 1
case <-quit:
fmt.Println("quit")
return
}
}
}
以后可以去深挖的一些东西:
go其他需要注意的features:
make
and new
func make(t Type, size ...IntegerType) Type
The make built-in function allocates and initializes an object of type slice, map, or chan (only). Like new, the first argument is a type, not a value. Unlike new, make's return type is the same as the type of its argument, not a pointer to it.
slice, map和chan其实underlying都是pointer,如果想pass by pointer可以直接作为参数传递而不需要传其pointer.
func new(Type) *Type
The new built-in function allocates memory. The first argument is a type, not a value, and the value returned is a pointer to a newly allocated zero value of that type.
defer
. 这份FAQ比较有用 https://pdos.csail.mit.edu/6.824/papers/tour-faq.txt
https://tour.golang.org/list
https://pdos.csail.mit.edu/6.824/papers/tour-faq.txt