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 关注
- 98 浏览
添加回答
举报