go 筆記
defer
defer 的參數綁定是在 defer 時,而不是在執行時,和 go 是一樣的。
for i := 0; i < 5; i++ {
defer fmt.Printf("%d ", i)
}
會輸出4 3 2 1 0
make && new
make 隻能應用于 slice,map,channel,傳回的不是指針。
以 slice 為例,一個 slice 是引用 copy,但是每個 slice 結構體戰三個字。
channel
range 用法,以及如何idiomatic的使用 closure
func handle(queue chan *Request) {
for r := range queue {
process(r)
}
}
func Serve(queue chan *Request) {
for req := range queue {
req := req // Create new instance of req for the goroutine.
sem <- 1
go func() {
process(req)
<-sem
}()
}
}
panic
panic可以用作複雜的錯誤處理,但是如果是用作錯誤處理,不要把 panic 暴露到包外。
Useful though this pattern is, it should be used only within a package. Parse turns its internal panic calls into error values; it does not expose panics to its client. That is a good rule to follow.
// error is a method of *Regexp that reports parsing errors by
// panicking with an Error.
func (regexp *Regexp) error(err string) {
panic(Error(err))
}
// Compile returns a parsed representation of the regular expression.
func Compile(str string) (regexp *Regexp, err error) {
regexp = new(Regexp)
// doParse will panic if there is a parse error.
defer func() {
if e := recover(); e != nil {
regexp = nil // Clear return value.
err = e.(Error) // Will re-panic if not a parse error.
}
}()
return regexp.doParse(str), nil
}