首页 > 其他分享 >Go - receiving from an empty channel

Go - receiving from an empty channel

时间:2023-09-27 17:14:41浏览次数:32  
标签:processItem Working go Go input receiving channel

 

package main

import (
    "fmt"
    "sync"
    "time"
)

var workers = 3

func processItem(input <-chan int, output chan<- int, wg *sync.WaitGroup) {
    for {
        fmt.Println("=")
        in := <-input
        fmt.Printf("Working on input: %v\n", in)
        time.Sleep(2 * time.Second)
        output <- in + 1
        wg.Done()
    }
}

func main() {
    items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    var wg sync.WaitGroup
    wg.Add(len(items))
    out := make(chan int, len(items))
    in := make(chan int)

    for i := 0; i < workers; i++ {
        go processItem(in, out, &wg)
    }

    for _, val := range items {
        in <- val
    }

    wg.Wait()

    total := 0
    for j := 0; j < len(items); j++ {
        total += <-out
    }
    fmt.Printf("Total sum is : %v\n", total)
}

 

zzh@ZZHPC:/zdata/MyPrograms/Go/ddd$ go run main.go
=
Working on input: 1
=
Working on input: 2
=
Working on input: 3
=
Working on input: 4
=
=
Working on input: 5
Working on input: 6
=
Working on input: 7
=
Working on input: 8
=
Working on input: 9
=
Working on input: 10
=
=
=
Total sum is : 65

"Working on input"输出有10个,"="输出有13个,说明函数processItem中的for循环是因为语句“in := <-input”而退出的(从空channel中接收不到数据会导致goroutine异常退出)。

 

标签:processItem,Working,go,Go,input,receiving,channel
From: https://www.cnblogs.com/zhangzhihui/p/17733125.html

相关文章

  • Go - ERROR: fatal error: all goroutines are asleep - deadlock!
    main.go:packagemainimport"fmt"funcmain(){ch:=make(chanint)ch<-1a:=<-chfmt.Println(a)}Goterror:zzh@ZZHPC:/zdata/MyPrograms/Go/testing$gorunmain.gofatalerror:allgoroutinesareasleep-deadlo......
  • Go - Live reload of configurations
    main.go:packagemainimport("encoding/json""fmt""log""os""time""github.com/fsnotify/fsnotify")typeconfigstruct{Namestring`json:"name"`}......
  • 一个思路:实现 golang 中的 `__file__` `__line__` 宏
    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!cnblogs博客zhihuGithub公众号:一本正经的瞎扯测试zaplog发现,开启caller的调用,会使整个服务增加2%的损耗。其实文件及其行号完全可以在编译期加上去,没必要带来运行期的性能损耗。因此有一个思路,可以解决这......
  • 2023-09-20:用go语言,保证一定是n*n的正方形,实现从里到外转圈打印的功能 如果n是奇数,中
    2023-09-20:用go语言,保证一定是n*n的正方形,实现从里到外转圈打印的功能如果n是奇数,中心点唯一,比如abcdefghie是中心点,依次打印:efihgdabc如果n是偶数,中心点为最里层2*2的右下点比如abcdefghijklmnopqrstuvwxyz0123456789最里层是opu......
  • 2023-09-16:用go语言,给你一个整数 n 和一个在范围 [0, n - 1] 以内的整数 p , 它们表示
    2023-09-16:用go语言,给你一个整数n和一个在范围[0,n-1]以内的整数p,它们表示一个长度为n且下标从0开始的数组arr,数组中除了下标为p处是1以外,其他所有数都是0。同时给你一个整数数组banned,它包含数组中的一些位置。banned中第i个位置表示arr[banned[i]]=......
  • goland编辑器编译的时候报错package xxx is not in GOROOT的原因排查
    先介绍下,我的目录部署情况1、GOROOT=C:\ProgramFiles\Go(我的golang环境装在c盘的)2、GOPATH=E:\Go(项目目录我放在E盘的)3、GO111MODULE=auto(默认值,没有改过)4、GOVERSION=go1.20.6(我的golang版本)5、项目结构,遵循官方推荐的方式E:\Go——bin——pkg——src 6、本次需要......
  • Django 使用模板语法编写新闻中心(爬虫获取数据)
    1.创建项目#创建项目django-adminstartprojectnews#进入项目目录cdnews#创建apppythonmanage.pystartappapp012.修改app2.1添加html进入app01文件夹在app01文件夹中添加templates文件夹在templates文件夹中添加index.html<!DOCTYPEhtml><......
  • 测试人员快速上手Django指南
    序言作为一个测试人员如果你有以下疑问,那么这篇文章将对你有很大帮助!1.网站的业务功能是如何实现的?参考章节:视图配置-实现网站业务功能2.服务器如何解析浏览器中的url?参考章节URL配置-提供url链接3.浏览器中的网页是如何展现给大家的?参考章节:模板-html页面的展示4.服务器如何与数据......
  • ABAP-MIGO保存修改数据增强
    三代增强:MB_DOCUMENT_BADI1METHODif_ex_mb_document_badi~mb_document_before_update.23FIELD-SYMBOLS:<fs_action>TYPEgodynpro-action,4<fs_refdoc>TYPEgodynpro-refdoc,5<lfs_xmseg>TYPEANYTABL......
  • golang 反射
    参考https://www.cnblogs.com/jiujuan/p/17142703.htmlfloat反射示例packagemainimport( "fmt" "reflect")funcmain(){ varxfloat64=1.2345 fmt.Println("==TypeOf==") t:=reflect.TypeOf(x) fmt.Println("type:&quo......