Using select, two cases can read data from the same Chan, and both cases may be executed to

package main

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

    go func() {
        for {
            select {
            case l := <-ch:
                Println (L) // this may be executed
            case l2 := <-ch:
                Println (L2) // it is also possible to execute this
    for {
        ch <- 1

Select details

Select as you understand it

  • Select only one channel with data for execution each time, and return to continue selecting after execution
  • Each select disrupts the case and executes from top to bottom until a channel with data is found
  • If the code in the case is time-consuming, you can use time. Sleep to test, and you won’t continue with the next selection.

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Ninety nine eighteen, one step at a time

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]