1 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
在你的情況下,你正在聽(tīng)linesChan,但沒(méi)有關(guān)閉它。當(dāng)所有數(shù)據(jù)都通過(guò)時(shí),您需要關(guān)閉此通道。done <- true不會(huì)被執(zhí)行。
但是這里不需要同步頻道,sync.WaitGroup{}就足夠了。
package main
import (
"fmt"
"sync"
"time"
)
type logEntry struct {
lines []string
created_at string
line_count int
}
var wg sync.WaitGroup
func main() {
linesChan := make(chan (logEntry))
// Process entries from lines
go func() {
for c := range linesChan {
time.Sleep(100 * time.Millisecond)
fmt.Printf("%v\n", c)
}
}()
// Read lines
for i := 1; i <= 10; i++ {
wg.Add(1)
go func(i int, linesChan chan (logEntry)) {
read(i, linesChan)
}(i, linesChan)
}
// Wait till all the files are read
wg.Wait()
}
func read(count int, channel chan (logEntry)) {
fmt.Println(count, "read")
channel <- logEntry{
line_count: count,
}
wg.Done()
}
- 1 回答
- 0 關(guān)注
- 120 瀏覽
添加回答
舉報(bào)