我是一個(gè) golang 新手,我遇到了一個(gè)相當(dāng)有趣的控制結(jié)構(gòu),它不遵循經(jīng)典的命令式 for 循環(huán)結(jié)構(gòu)。我也無(wú)法找到有關(guān)該結(jié)構(gòu)的文檔。以下是有問(wèn)題的代碼: 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) } } }循環(huán)構(gòu)造是永遠(yuǎn)循環(huán)還是幕后有事件系統(tǒng)綁定?
無(wú)條件的 { ..block.. } 的 golang 有什么作用?
達(dá)令說(shuō)
2021-12-07 14:56:56