Effective Go阅读纪要

Formatting

Commentary

  1. godoc regexp | grep -i compile

    在可导出函数上的注释是可以搜索的
  2. https://golang.org/doc/effective_go.html#redeclaration

defer

defer is LIFO order

for i := 0; i < 5; i++ {
    defer fmt.Printf("%d ", i)
}
#output
4 3 2 1 0

defer中函数的执行顺序还需要加深理解:https://golang.org/doc/effective_go.html#defer

Allocation

new vs make

composite literal

new(File) and &File{} are equivalent

make :creates slices, maps, and channels only

Maps

Like slices, maps hold references to an underlying data structure. If you pass a map to a function that changes the contents of the map, the changes will be visible in the caller.

For maps the keys may be output in any order

Append

But what if we wanted to do what our Append does and append a slice to a slice? Easy: use ... at the call site, just as we did in the call to Output above. This snippet produces identical output to the one above.

x := []int{1,2,3}
y := []int{4,5,6}
x = append(x, y...)
fmt.Println(x)

Constants

They are created at compile time.

Because of the compile-time restriction, the expressions that define them must be constant expressions, evaluatable by the compiler. For instance, 1<<3 is a constant expression, while math.Sin(math.Pi/4) is not because the function call to math.Sin needs to happen at run time.

init function

Pointers vs. Values

methods can be defined for any named type (except a pointer or an interface)

values for receivers is that value methods can be invoked on pointers and values, but pointer methods can only be invoked on pointers

When the value is addressable, the language takes care of the common case of invoking a pointer method on a value by inserting the address operator automatically. In our example, the variable b is addressable, so we can call its Write method with just b.Write. The compiler will rewrite that to (&b).Write for us.?

Interfaces

Conversions

接口类型断言,要加上异常判断,即接口是否含有这个类型

Generality

Embedding

https://golang.org/doc/effective_go.html#embedding

important

Concurrency

goroutine

it is a function executing concurrently with other goroutines in the same address space

Parallelization

bookmark

done

conslusion

This article introduces some inport concept about golang: Goroutine interface channel make/new.

Go is a current specific lang.

comments powered by Disqus