[Go] golang的range循环遍历通道
range循环会无限在channels上面迭代
package main import (
"fmt"
"time"
) func main() { //创建一个通道
ch := make(chan int)
//开启一个goroutine
go func() {
//无限循环
for {
//往通道里发送数据
ch <- 1
//睡眠一秒
time.Sleep(time.Second)
}
}()
//无限循环从通道中读取数据
for i := range ch {
fmt.Println(i)
} }
和下面的无限读取是等价的
package main import (
"fmt"
"time"
) func main() { //创建一个通道
ch := make(chan int)
//开启一个goroutine
go func() {
//无限循环
for {
//往通道里发送数据
ch <- 1
//睡眠一秒
time.Sleep(time.Second)
}
}()
//无限循环从通道中读取数据
for {
i, ok := <-ch
if !ok {
break
}
fmt.Println(i)
}
}
[Go] golang的range循环遍历通道的更多相关文章
- swift基本用法-for循环遍历,遍历字典,循环生成数组
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- Python的list循环遍历中,删除数据的正确方法
在遍历list,删除符合条件的数据时,总是报异常,代码如下: num_list = [1, 2, 3, 4, 5] print(num_list) for i in range(len(num_lis ...
- swift-for循环遍历,遍历字典,循环生成数组
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- golang——关于for循环的学习
1.for循环的用法 (1)常规用法 func main() { slice := []int{1, 2, 3, 4, 5, 6} //方式1 for i := 0; i < len(slice ...
- To Java程序员:切勿用普通for循环遍历LinkedList
ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...
- *使用while循环遍历数组创建索引和自增索引值
package com.chongrui.test;/* *使用while循环遍历数组 * * * */public class test { public static void main ...
- 【转】ArrayList循环遍历并删除元素的常见陷阱
转自:https://my.oschina.net/u/2249714/blog/612753?p=1 在工作和学习中,经常碰到删除ArrayList里面的某个元素,看似一个很简单的问题,却很容易出b ...
- sqlserver中的循环遍历(普通循环和游标循环)
sql 经常用到循环,下面介绍一下普通循环和游标循环 1.首先需要一个测试表数据Student
- php中的循环遍历 foreach list each
foreach语句遍历数组foreach语句用于循环遍历数组,每进行一次循环,当前数组元素的值就会被赋值给变量value(也可以是其它变量),数组指针会逐一的移动. 代码示例: foreach($ar ...
随机推荐
- 04.封装ajax
<script> //封装ajax // 函数名 ajax // 函数的参数 // url: 请求的地址 // type: 请求的方式 get /post // data: 要上传的数据 ...
- async ,await 有图有真相
1.async返回的一定是promise对象 2.await确实可以同步:
- HBuilder git使用-环境配置
HBuilder中使用的是Egit插件,但提供的相关资料太少,这是目前遇到的一些问题的总结 1. 安装好egit插件后,本机需要安装Git windows的安装程序,并配置好相关的环境变量(理论上是自 ...
- [.net 面向对象程序设计深入](31)实战设计模式——使用Ioc模式(控制反转或依赖注入)实现松散耦合设计(1)
[.net 面向对象程序设计深入](31)实战设计模式——使用IoC模式(控制反转或依赖注入)实现松散耦合设计(1) 1,关于IOC模式 先看一些名词含义: IOC: Inversion of con ...
- Redis Cluster(集群)
一.概述 在前面的文章中介绍过了redis的主从和哨兵两种集群方案,redis从3.0版本开始引入了redis-cluster(集群).从主从-哨兵-集群可以看到redis的不断完善:主从复制是最简单 ...
- 【RL-TCPnet网络教程】第30章 RL-TCPnet之SNTP网络时间获取
第30章 RL-TCPnet之SNTP网络时间获取 本章节为大家讲解RL-TCPnet的SNTP应用,学习本章节前,务必要优先学习第29章的NTP基础知识.有了这些基础知识之后,再搞本章节会 ...
- [Swift]LeetCode270. 最近的二分搜索树的值 $ Closest Binary Search Tree Value
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [Swift]LeetCode645. 错误的集合 | Set Mismatch
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- [Swift]LeetCode790. 多米诺和托米诺平铺 | Domino and Tromino Tiling
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...
- [Swift]LeetCode917. 仅仅反转字母 | Reverse Only Letters
Given a string S, return the "reversed" string where all characters that are not a letter ...