golang中超时处理

package main

import (
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan int)
	quit := make(chan bool)

	go func() {
		for{
			select {
			case <-ch1:
				fmt.Printf("读取到数据\n")
				break
			case <-time.After(3*time.Second):
				fmt.Printf("超时拉\n")
				quit<-true
				goto lable
			}
			fmt.Printf("=============\n")
		}
		lable:
	}()


	for i:=0;i<2;i++{
		ch1<-i
		time.Sleep(2*time.Second)
	}

	<-quit
	fmt.Printf("完毕")

}

发布了74 篇原创文章 · 获赞 2 · 访问量 6477

猜你喜欢

转载自blog.csdn.net/weixin_42067668/article/details/103483617