首页 > 其他分享 >go 语言 channel for select

go 语言 channel for select

时间:2022-08-21 23:33:08浏览次数:58  
标签:10 int chan intchan select go channel

示例demo55 package main
import (   "fmt"   "time" )
func main() {   intChan := make(chan int, 10) //初始化 intchan 通道 长度10   intChan2 := make(chan int, 10) //初始化 intchan 通道 长度10
  go func() {     for i := 0; i < 10; i++ {       intChan <- i       intChan2 <- i     }   }()   //无限循环 不阻塞 没有数据就会到default   for {     select {     case v := <-intChan:       fmt.Println(v)     case v := <-intChan2:       fmt.Println(v)     default:       fmt.Println("get data timeout")       time.Sleep(time.Second)     }   }
}

标签:10,int,chan,intchan,select,go,channel
From: https://www.cnblogs.com/chenweihao/p/16611402.html

相关文章

  • go 语言 goroutine通信
    示例demo52packagemainimport("fmt")funccalc(taskChanchanint,resChanchanint,exitChanchanbool){forv:=rangetaskChan{......
  • golang语法速记
    Golang语言中存在一个关键字type,type又有两种使用方式,一种是类型别名,一种是类型定义。GoLang1.9后对內建定义类型使用了新的写法:typeNewName=Type这个NewName只是Typ......
  • Golang基础入门
    基础入门1、输出输出,在运行代码时将内容在终端输出。packagemainimport"fmt"funcmain(){fmt.Println("helloword!")}在Go中提供了多种输出的方式......
  • Django 2.0 新特点
    2017年12月2日,Django官方发布了2.0版本,成为多年来的第一次大版本提升,那么2.0对广大Django使用者有哪些变化和需要注意的地方呢?一、Python兼容性Django2.0支持Python3.......
  • Golang的IDE的安装
    Golang的IDE的安装Goland是一款由JetBrains公司开发的软件,使用他可以大大提高程序员开发Go代码的效率,因为Goland内部提供了编辑器、调试器和图形用户界面等很多方便的功......
  • golang反射reflect
    1reflect包reflect包实现了运行时反射,允许程序操作任意类型的对象。典型用法是用静态类型interface{}保存一个值,通过调用TypeOf获取其动态类型信息,该函数返回一个Type类......
  • PicGo+GitHub 图床搭建
    前言用GitHub搭建图床,在很久之前我就有了解,但由于市面上有挺多免费的图床,比如我之前一直在用的路过图床,所以一直懒得动手搭建GitHub图床。一直到前两天我在完善博客的相......
  • go 语言 chan读写数据
    示例demo51packagemainimport("fmt""time")funcsendData(chchanint){//把数据写到通道里fori:=0;i<20;i++{......
  • [Google] LeetCode 2013 Detect Squares
    YouaregivenastreamofpointsontheX-Yplane.Designanalgorithmthat:Addsnewpointsfromthestreamintoadatastructure.Duplicatepointsareallow......
  • golang 值类型与引用类型
    转自:https://www.zhihu.com/search?type=content&q=golang%20%20%E5%80%BC%E7%B1%BB%E5%9E%8B%E3%80%81%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B%E3%80%81%E6%8C%87%E9%92%88......