1 回答

TA貢獻1829條經驗 獲得超6個贊
在你的情況下,你正在聽linesChan,但沒有關閉它。當所有數據都通過時,您需要關閉此通道。done <- true不會被執行。
但是這里不需要同步頻道,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 關注
- 121 瀏覽
添加回答
舉報