在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Channel常用与goroutine之间传递消息和数据
Channel基础用法: package main import ( "fmt" "time" ) func worker(id int, ch chan int) { for n := range ch{ fmt.Printf("Worker %d received %d\n", id, n) } } func createWorker(id int) chan<- int { ch := make(chan int) go worker(id, ch) return ch } func chanDemo() { var channels [10]chan<- int for i := 0; i < 10; i++ { channels[i] = createWorker(i) } for i := 0; i < 10; i++ { channels[i] <- 'a' + i } for i := 0; i < 10; i++ { channels[i] <- 'A' + i } time.Sleep(time.Millisecond) } func bufferedChannel() { ch := make(chan int, 3) go worker(0, ch) ch <- 'a' ch <- 'b' ch <- 'c' ch <- 'd' time.Sleep(time.Millisecond) } func channelClose() { ch := make(chan int) go worker(0, ch) ch <- 'a' ch <- 'b' ch <- 'c' ch <- 'd' close(ch) time.Sleep(time.Millisecond) } func main() { fmt.Println("Channel as firts-class citizen") chanDemo() fmt.Println("Buffered channel") bufferedChannel() fmt.Println("Channel close and range") channelClose() }
Channel 任务等待 package main import ( "fmt" "sync" ) func doWorker(id int, ch chan int, wg *sync.WaitGroup) { for n := range ch { fmt.Printf("Worker %d received %c\n", id, n) wg.Done() } } type worker struct { in chan int wg *sync.WaitGroup } func createWorker(id int, wg *sync.WaitGroup) worker { w := worker{ in: make(chan int), wg: wg, } go doWorker(id, w.in, wg) return w } func chanDemo() { var wg sync.WaitGroup var workers [10]worker for i := 0; i < 10; i++ { workers[i] = createWorker(i, &wg) } for i, worker := range workers { worker.in <- 'a' + i wg.Add(1) // 添加一个任务 } for i, worker := range workers { worker.in <- 'A' + i wg.Add(1) // 添加一个任务 } wg.Wait() } func main() { // sync.WaitGroup 等待多任务结束 chanDemo() }
|
请发表评论