select

package main

import (
"fmt"
"time"
// "math/rand"
)

// this is a comment
func main() {
c1 := make(chan string)
c2 := make(chan string)

go func() {
for {
c1 <- "from 1"
time.Sleep(time.Second * 2)
}
}()
go func() {
for {
c2 <- "from 2"
time.Sleep(time.Second * 3)
}
}()
go func() {
for {
select {
case msg1 := <- c1:
fmt.Println(msg1)
case msg2 := <- c2:
fmt.Println(msg2)
}
}
}()

var input string
fmt.Scanln(&input)
}

原文地址:https://www.cnblogs.com/rojas/p/4350367.html