Other examples:

Channels can be categorized as send-and-receive, read-only, and send-only according to whether the internal recvq and sendq queues are permitted to store the corresponding waiters, the goroutines.

The key is to keep in mind the core of using channels, the signal transmission between goroutines. 2

image

One of the main features of Go programming language is its eponymous go statement. In Go, it’s super-easy to start a new goroutine. But how do you get its results? How do you know if it may have errored? How do you wait for it to complete? How do you cancel it, if you don’t need its results anymore? Those familiar with Go will say: well, obviously, use channels. But channel in Go is still a low level construct. The more channels you have — the more problems. Channels can deadlock. Channels can panic. 3

Different languages and frameworks have a design pattern to defer value. Some call it Future, other call it Promise, and a bunch of other names. A go statement is eager by its nature. 3

Channel

In Go, channels are used to communicate between goroutines. Channels are like pipes through which you can pass data. However, when we try to read from a channel that has no data, it can cause the program to block. To avoid blocking, Go provides a built-in mechanism that allows us to detect if a channel is closed or if there is no data available. This mechanism is called the “comma ok” syntax.

The “comma ok” syntax is a Go idiom that is used to check if a channel is closed or if there is data available to read from the channel. The syntax is as follows:

  • value, ok := <-channel
import "fmt"

func main() {
  ch := make(chan int)

  go func() {
    fmt.Println("2")
    ch <- 42
    fmt.Println("3")
    ch <- 10
    fmt.Println("6")
    close(ch)
    fmt.Println("7")
  }()

  fmt.Println("1")
  value, ok := <-ch

  fmt.Println("4")
  fmt.Println(value, ok)

  value, ok = <-ch
  fmt.Println("5")
  fmt.Println(value, ok)

  value, ok = <-ch
  fmt.Println("8")
  fmt.Println(value, ok)

  // output:
  // 1
  // 2
  // 3
  // 4
  // 42 true
  // 5
  // 10 true
  // 6
  // 7
  // 8
  // 0 false
}

When to Use Channel 2

  • trigger signal, including end and start
  • transfer data in async. Asynchronous worker processes one by one for the non-urgent
  • block for purpose. For critical steps, you need to block and wait
  • the worker pool. The worker is either woken up by the task or long blocked until the task comes