jasperzhong / cs-notes

CS认知体系
6 stars 0 forks source link

Learn Golang #25

Open jasperzhong opened 2 years ago

jasperzhong commented 2 years ago

https://tour.golang.org/list

https://pdos.csail.mit.edu/6.824/papers/tour-faq.txt

jasperzhong commented 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
        }
    }
}

以后可以去深挖的一些东西:

  1. goroutine实现,以及和thread区别
  2. channel实现
  3. select实现

go其他需要注意的features:

  1. implicit implement (隐式继承)
  2. explicit type conversion (显示类型转换)
  3. slice用法 (length, capacity)
  4. memory allocation: 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.

  1. for-range loop (a cool feature)
  2. defer.

这份FAQ比较有用 https://pdos.csail.mit.edu/6.824/papers/tour-faq.txt