我是一個golang初學者,我遇到了一個非常有趣的控制結構,它不遵循經典命令for-loop結構。我一直無法找到有關結構的文檔。以下是有問題的代碼:沒有條件的{..block ..}的golang會有什麼作用?
for {
// read each incoming message
m, err := getMessage(ws)
if err != nil {
log.Fatal(err)
}
// see if we're mentioned
if m.Type == "message" && strings.HasPrefix(m.Text, "<@"+id+">") {
// if so try to parse if
ans := lookup(session, m.Text)
if len(ans)>0 {
// looks good, get the quote and reply with the result
go func(m Message) {
for _, def := range ans {
if len(def[1]) > 0 {
m.Text = "*" + def[0] + " " + def[1] + "*: " + def[2]
} else {
m.Text = "*" + def[0] + "*: " + def[2]
}
postMessage(ws, m)
}
}(m)
// NOTE: the Message object is copied, this is intentional
} else {
// huh?
m.Text = fmt.Sprintf("sorry, that does not compute\n")
postMessage(ws, m)
}
}
}
循環構造是否只是永遠循環或在場景中是否存在事件系統綁定?
*循環結構是否永遠循環*是的。 –
在[Tour of Go](http://tour.golang.org/flowcontrol/3)和[Effective Go](https://golang.org/doc/effective_go.html#for) – JimB