首页 > 其他分享 >go 语言 chan读写数据

go 语言 chan读写数据

时间:2022-08-21 19:13:00浏览次数:75  
标签:10 ch int 读写 chan func go

示例demo51 package main
import (   "fmt"   "time" )
func sendData(ch chan int) {   //把数据写到通道里   for i := 0; i < 20; i++ {     ch <- i     fmt.Println("push data:", i)   } }
func getData(ch chan int) {   //从通道里读取数据   var b int   for {     b = <-ch     fmt.Println("get data:", b)     time.Sleep(time.Second)   } }
func main() {   ch := make(chan int, 10) //定义一个int类型chan 长度为10 写到10满了 就写不进去了 缓冲区就是10   go sendData(ch) //数据写到chan   go getData(ch) //从chan读取数据   time.Sleep(10 * time.Second) //休眠10秒,如果不休眠,主进程很快结束就看不到协程输出的值的 }

标签:10,ch,int,读写,chan,func,go
From: https://www.cnblogs.com/chenweihao/p/16610578.html

相关文章

  • [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......
  • Mongodb使用总结
    Mongodb使用总结基于内存操作,便于与网站交互数据库-集合-文档(存储多种数据类型),我们的操作都是基于单文档进行操作,并且通过冗余字段进行操作嵌入式数组文档减少了对昂贵......
  • 【Go学习】Stings
    strings.HasPrefix(sstring,prefixstring)bool:判断字符串s是否以prefix开头。de13strings.HasSuffix(sstring,suffixstring)bool:判断字符串s是否以suffix结尾。a......
  • Golang学习(二)
    12.单元测试12.1引入在我们工作中有时需要去确认一个函数或者一个模块的结果是否正确,如:  12.2传统的方法解决问题在main函数中调用addUpper函数,看看实际输出结果......
  • git pull 拉代码冲突提示“error: Your local changes to the following files would
    gitpull拉代码冲突提示“error:Yourlocalchangestothefollowingfileswouldbeoverwrittenbymerge:        practice/fun.pyPleasecommityourcha......
  • Fisher Information Matrix when Changing Variables
    CopiedfromthislinkFornormal\(X\simN(\mu,\sigma^2)\),informationmatrixis\[\mathcal{I}_1=\left(\begin{matrix}\frac{1}{\sigma^2}&0\\0&\frac{1......
  • 新人使用Gorm的踩坑总结
    在使用Update更新数据时一定要将where条件放在update前面,否则where不会生效,将更新所有数据正确的写法//条件更新db.Model(&User{}).Where("id=?",ID).Update("name",......
  • Golang中slice操作的一些坑
    packagego_testsimport("bytes""fmt""testing")//0、引用类型funcTestT0(t*testing.T){s1:=[]int{1,2,3}f1:=func(s[]int......
  • Notepad plus 通过NppExec插件编译/运行 golang,php,python等语言
        1. 在Notepadplus的插件-->插件管理中,添加nppExec插件。          2.打开插件-->NppExec,选择Showconsole,和Follow($CURRE......