GO chan select

本文介绍Go语言中的select关键字,它允许监听多个channel并在数据可用时进行处理。通过一个示例展示了如何创建并行处理两个channel,将接收到的值传递到worker channel,并在worker channel中打印这些值。此外,还提及了select与定时器的结合使用。

select功能:
可以在select中监听多个chan,哪个chan来了就处理哪个,这样的好处是可以对多个chan进行并行处理。
下面给出一个例子,例子的功能是:
1、创建两个chan并向其中每间隔1.5秒写入一个值,chan收到值后直接写入worker chan中
2、worker chan收到值直接输出
3、select中加入了一些定时器的用法,供参考

main.go

package main

import (
	"fmt"
	"time"
)

func createIntChan() chan int{
	c := make(chan int)
	i := 0
	go func() {
		for{
			time.Sleep(time.Millisecond*800)
			c <- i
			i++
		}
	}()
	return c
}

func createWork() chan string {
	worker := make(chan string)
	return worker
}
func inputWork(n int,worker chan string,chanName string)  {
	v := fmt.Sprintf( "the value is %d from chan %s",n,chanName)
	worker <- v
}
func main() {
	c1,c2 := createIntChan(),createIntChan()
	worker := createWork()

	tm := time.After(time.Second*10)
	tx := time.Tick(time.Second)
	for{
		select {
		//10秒后关闭应用
		case <-tm:
			fmt.Println("bye ...")
			return
		//schedule:每间隔两2秒打印一次
		case <-tx:
			fmt.Println("间隔2秒打印。。。")
		case n := <- c1:
			go inputWork(n,worker,"c1")
		case n := <-c2:
			go inputWork(n,worker,"c2")
		case v := <- worker:
			fmt.Println("out put from cache : ",v)
		//300毫秒没收到任何chan的值就打印这条
		case <-time.After(time.Millisecond*300):
			fmt.Println("time out")
		}
	}
}
### Go语言 `chan` 的使用教程及示例 #### 什么是 `chan` 在Go语言中,`chan` 是一种用于goroutine之间通信的机制。它允许在一个goroutine中发送数据到另一个goroutine中接收该数据[^3]。 #### 声明与初始化 可以通过两种方式创建一个通道: 1. **变量声明**: ```go var channelName chan int ``` 2. **使用 `make` 函数初始化**: ```go channelName := make(chan int, buffer_size) ``` 其中,`buffer_size` 表示缓冲区大小。如果未指定,则默认为无缓冲通道[^4]。 #### 数据传输方向 可以定义只读或只写类型的通道来限制其行为: - 只读通道:`<-chan T` - 只写通道:`chan<- T` 这有助于提高程序的安全性和可维护性。 #### 示例代码 以下是几个典型的 `chan` 使用场景: ##### 1. 单向通信(无缓冲) ```go package main import "fmt" func sendData(ch chan<- string) { ch <- "Hello from sender" } func receiveData(ch <-chan string) { message := <-ch fmt.Println("Received:", message) } func main() { channel := make(chan string) go sendData(channel) receiveData(channel) } ``` 此示例展示了如何通过单向通道进行简单的消息传递。 ##### 2. 缓冲通道 当需要处理大量并发请求时,缓冲通道能有效减少阻塞的发生。 ```go package main import ( "fmt" "time" ) func producer(ch chan<- int) { for i := 0; i < 5; i++ { ch <- i time.Sleep(time.Second / 2) } close(ch) } func consumer(ch <-chan int) { for value := range ch { fmt.Printf("Consumed %d\n", value) } } func main() { bufferedChannel := make(chan int, 3) go producer(bufferedChannel) consumer(bufferedChannel) } ``` 在此例子中,生产者不断往通道里放入整数,消费者则逐一取出并打印出来。 ##### 3. 超时控制 利用 `select` 和定时器实现超时逻辑。 ```go package main import ( "fmt" "sync" "time" ) func worker(done chan bool, wg *sync.WaitGroup) { defer wg.Done() time.Sleep(2 * time.Second) done <- true } func main() { var wg sync.WaitGroup done := make(chan bool, 1) timeout := time.After(3 * time.Second) wg.Add(1) go worker(done, &wg) select { case <-done: fmt.Println("Worker completed successfully.") case <-timeout: fmt.Println("Operation timed out.") } wg.Wait() } ``` 这段代码演示了如何设置工作完成与否的时间界限。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值