package main
import (
"fmt"
"time"
)
type MyStruct struct {
Field1 string
Field2 int
}
func main() {
ch1 := make(chan string)
ch2 := make(chan int)
ch3 := make(chan MyStruct)
go func() {
// 模拟向通道发送数据,这里可以根据实际需求修改
time.Sleep(5 * time.Second)
ch1 <- "Hello from ch1"
}()
go func() {
time.Sleep(8 * time.Second)
ch2 <- 42
}()
go func() {
time.Sleep(12 * time.Second)
ch3 <- MyStruct{Field1: "struct data", Field2: 100}
}()
select {
case data1 := <-ch1:
fmt.Printf("Received from ch1: %s\n", data1)
case data2 := <-ch2:
fmt.Printf("Received from ch2: %d\n", data2)
case data3 := <-ch3:
fmt.Printf("Received from ch3: %+v\n", data3)
case <-time.After(10 * time.Second):
fmt.Println("操作超时")
return
}
}