hic003cih / Golang

0 stars 0 forks source link

2019.10.17 #12

Open hic003cih opened 4 years ago

hic003cih commented 4 years ago

Channels Understanding channels Channels Introduction making a channel c := make(chan int) putting values on a channel c <- 42 taking values off of a channel <-c buffered channels c := make(chan int, 4) channels block they are like runners in a relay race they are synchronized they have to pass/receive the value at the same time just like runners in a relay race have to pass / receive the baton to each other at the same time one runner can’t pass the baton at one moment and then, later, have the other runner receive the baton the baton is passed/received by the runners at the same time the value is passed/received synchronously; at the same time channels allow us to pass values between goroutines code: doesn’t work https://play.golang.org/p/XPgsj2xS0F IMPORTANT: CHANNELS BLOCK channels allow coordination / synchronization / orchestration buffering (buffered channels) send & receive https://play.golang.org/p/SHr3lpX4so buffer https://play.golang.org/p/hsttb2qEJi “The capacity, in number of elements, sets the size of the buffer in the channel. If the capacity is zero or absent, the channel is unbuffered and communication succeeds only when both a sender and receiver are ready.” Golang Spec buffer doesn’t work https://play.golang.org/p/epLsvcivJS buffer https://play.golang.org/p/_6bSl5fc17 code: https://github.com/GoesToEleven/go-programming