1 回答

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊
每次啟動(dòng) goroutine 后,原始代碼會(huì)阻塞,等待通過(guò)非緩沖通道發(fā)送一個(gè)值,此外,當(dāng)WaitGroup
倒計(jì)時(shí)時(shí)通道關(guān)閉,這也關(guān)閉了接收端的通道。
恕我直言,一般規(guī)則是:
不要從接收方關(guān)閉通道,如果通道有多個(gè)并發(fā)發(fā)送方,也不要關(guān)閉通道。
package main
import (
"fmt"
"strings"
)
type StratNotification struct {
Symbol string
}
func GetSymbols() []StratNotification {
return []StratNotification{
{Symbol: "a"},
{Symbol: "b"},
{Symbol: "c"},
{Symbol: "d"},
}
}
func RunIntradayScanner() {
symbols := GetSymbols()
var intradayChannel = make(chan StratNotification)
for _, s := range symbols {
go IntradayStratify(strings.TrimSpace(s.Symbol), intradayChannel)
}
for _ = range symbols {
s := <-intradayChannel
fmt.Println(s)
}
}
func IntradayStratify(ticker string, c chan StratNotification) {
// do some heavy lifting
fmt.Println(ticker)
c <- StratNotification{}
}
func main() {
RunIntradayScanner()
}
- 1 回答
- 0 關(guān)注
- 109 瀏覽
添加回答
舉報(bào)