ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

go channel理解不彻底遇到的问题-nil channel

2021-12-27 10:33:42  阅读:113  来源: 互联网

标签:nil int chan func time go channel out


代码A

package main

import (
	"fmt"
	"time"
)

func generator() chan int {
	out := make(chan int)
	go func() {
		i := 0
		for {
			//time.Sleep(time.Duration(rand.Intn(8500)) * time.Millisecond)
			time.Sleep(1 * time.Second)
			out <- i
			i++
		}
	}()
	return out
}

func worker(id int, c chan int) {
	for n := range c {
		time.Sleep(time.Second)
		fmt.Printf("Worker %d received %d\n",
			id, n)
	}
}

func createWorker(id int) chan<- int {
	c := make(chan int)
	go worker(id, c)
	return c
}

func main() {
	var c1, c2 = generator(), generator()
	var workChannel = createWorker(0)

	var values []int
	tm := time.After(10 * time.Second)
	tick := time.Tick(time.Second)
	for {
		var activeWorker chan<- int
		var activeValue int
		if len(values) > 0 {
			activeWorker = workChannel
			activeValue = values[0]
		}

		select {
		case n := <-c1:
			println("c1")
			values = append(values, n)
		case n := <-c2:
			println("c2")
			values = append(values, n)
		case activeWorker <- activeValue:
			println(activeValue)
			fmt.Println("values :", values, " activeValue:", activeValue)
			println(len(values))
			values = values[1:]

		case <-time.After(800 * time.Millisecond):
			fmt.Println("timeout")
		case <-tick:
			fmt.Println(
				"queue len =", len(values))
		case <-tm:
			fmt.Println("bye")
			return
		}
	}
}

  代码B

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func generator() chan int {
	out := make(chan int)
	go func() {
		i := 0
		for {
			time.Sleep(time.Duration(rand.Intn(1500)) * time.Millisecond)
			//time.Sleep(1 * time.Second)
			out <- i
			i++
		}
	}()
	return out
}

func worker(id int, c chan int) {
	for n := range c {
		time.Sleep(time.Second)
		fmt.Printf("Worker %d received %d\n",
			id, n)
	}
}

func createWorker(id int) chan<- int {
	c := make(chan int)
	go worker(id, c)
	return c
}

func main() {
	var c1, c2 = generator(), generator()
	var workChannel = createWorker(0)

	var values []int
	//tm := time.After(10 * time.Second)
	tick := time.Tick(time.Second)
	for {
		// var activeWorker chan<- int
		var activeValue int
		if len(values) > 0 {
			// activeWorker = workChannel
			activeValue = values[0]
		}

		select {
		case n := <-c1:
			values = append(values, n)
		case n := <-c2:
			values = append(values, n)
		case workChannel <- activeValue:
			fmt.Println("values :", values, " activeValue:", activeValue)
			values = values[1:]
		case <-tick:
			fmt.Println(
				"queue len =", len(values))
			//case <-tm:
			//	fmt.Println("bye")
			//	return
		}
	}
}

  代码A、B 晃眼一看基本没有什么问题,都是两个生产者一个消费者,A只是多了 activeWorker 的临时变量,中间做了转换,但是问题就出在这里,由于对于nil channel的理解不够,所以一直认为 nil channel是可以传值的,但是百思不得其解,为什么一直报错。

      网上找了教程,看了总结,才发现,原理竟然如此简单,论基础的重要性。。。。。。

 

标签:nil,int,chan,func,time,go,channel,out
来源: https://www.cnblogs.com/hoganhome/p/15735301.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有