go channel puzzle

Keith · · 1498 次点击
for the print order. when you use goroutine,it's hard to say which goroutine will finish first. that's why sometime we use channel to make few goroutine in a order. you can use more goroutine in your test4 and run it few time ,then you can see order not always the same here a example use channel as lock ``` package main import ( "fmt" "time" ) var c1 chan int func step1() { fmt.Println("step 1") c1 <- 1 } func step2() { <-c1 fmt.Println("step 2") } func main() { c1 = make(chan int) go step2() go step1() time.Sleep(10) } ```
#2
更多评论
it's like a queue. `arr1 := make(chan string)` you can put max one member into this chan,in order to push another one ,you should pop it first! `arr2 := make(chan string, 10)` you can push 10 member ,before you push 11th you should pop some member the number means max n in the chan at the same time.
#1