• golang中通过循环管道来取值


    golang中管道(chan)的一些用法

    golang中的管道可以用于各个goroute方法的数据交互,管道有一些方法:

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    	var (
    		IntChan = make(chan int, 1000)
    		//ResultChan = make(chan int, 1000)
    	)
    
    	for i := 0; i < 10; i++ {
    		IntChan <- i
    	}
    	close(IntChan)  // 关闭管道,否则下面的for循环会一直循环下去
    	fmt.Println("循环管道取值前管道内容个数: ", len(IntChan))
    
    	for v := range IntChan { // 循环管道: 循环一次即相当于从中取出一个值,管道内的值取空则退出循环
    		fmt.Println(v)
    	}
    	fmt.Println("循环管道取值后管道内容个数: ", len(IntChan))
    
    	//IntChan <- 520  管道已经关闭了,无法插入值了
    	fmt.Println("循环完之后管道中的值: ", <-IntChan)
    	fmt.Println("循环完之后管道中的值: ", <-IntChan)
    }
    
    
    循环管道取值前管道内容个数:  10
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    循环管道取值后管道内容个数:  0
    循环完之后管道中的值:  0
    循环完之后管道中的值:  0
    
  • 相关阅读:
    python之shutil模块
    python的os模块
    python的map函数
    Web基础知识
    Web基础知识 --- html中的meta元素有什么用?
    使用技巧 --- 与 FireFox 相关
    基础知识之WIN32 API
    资料索引
    基础知识之C++篇
    使用技巧 --- 与 Visual Studio 有关
  • 原文地址:https://www.cnblogs.com/shuchengyi/p/11409197.html
Copyright © 2020-2023  润新知