我是一个 golang 新手,我遇到了一个相当有趣的控制结构,它不遵循经典的命令式 for 循环结构。我也无法找到有关该结构的文档。以下是有问题的代码:
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)
}
}
}
循环构造是永远循环还是幕后有事件系统绑定?
GCT1015
潇潇雨雨
相关分类