Golang中缓冲通道的问题探讨
Golang中缓冲通道的问题探讨 我不明白为什么缓冲通道只返回一个项目。 Playground: playground
package main
import ( “fmt” “sync” )
func Func1(wg *sync.WaitGroup, c chan string) { c <- “one1” c <- “one2” wg.Done() }
func Func2(wg *sync.WaitGroup, c chan string) { c <- “two1” c <- “two2” c <- “two3” wg.Done() }
func main() { var wg sync.WaitGroup var results []string = make([]string, 20)
c1 := make(chan string, 10) c2 := make(chan string, 10) wg.Add(2) go Func1(&wg, c1) go Func2(&wg, c2)
wg.Wait() close(c1) close(c2)
results = append(results[:], <-c1)
results = append(results[:], <-c2)
fmt.Printf(“size=%d, cap=%d, type=%T v=%v\n”, len(results), cap(results), results, results) for p, pv := range results { fmt.Println(p, pv) } }
更多关于Golang中缓冲通道的问题探讨的实战教程也可以访问 https://www.itying.com/category-94-b0.html
你只在每次追加时从它读取一次。尝试:
for v := range c1 {
results = append(results, v)
}


