douli1306 2018-08-08 07:32
浏览 134
已采纳

Golang:在多个goroutine中发送关闭通道错误

I have 3 channels that get data from together . My code run for first time perfect but when after that I send another data , there is error : send on closed channel.In func FillNotCheckedDeliveryCh that line I closed channel .If I do not close it app stopped and not continue.

func main() {
    receiveBulkIdsCh := make(chan int64, 100)
    NotCheckedDeliveryCh := make(chan CommonType.BasicRequestParameters, 100)
    ResultCh := make(chan CommonType.MessageStateResult, 100)

    var wg sync.WaitGroup
    wg.Add(4)

    /* Every BulkIds that receive from queue , save in  receiveBulkIdsChan channel*/
    go func() {
        defer wg.Done()
        PopQueue(receiveBulkIdsCh)
    }()
    go func() {
        defer wg.Done()
        for BulkId := range receiveBulkIdsCh {
            FillNotCheckedDeliveryCh(data, NotCheckedDeliveryCh)
        }
    }()
    go func() {
        defer wg.Done()
        for item := range NotCheckedDeliveryCh {
            for msgStatus := range DoFuncGetMessageState(item) {
                ResultCh <- msgStatus
            }
            close(ResultCh)
        }
    }()
    go func() {
        defer wg.Done()
        for Result := range ResultCh {
            ReadResultCh(Result)
        }
    }()
    wg.Wait()
}

func FillNotCheckedDeliveryCh(data IntegrateRowsFields, ch chan<- CommonType.BasicRequestParameters) {
    ch <- PackerForNotFinishedCh(data)
    defer close(ch)
}

can help what's wrong ?

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 session失效问题
      • ¥15 GMS地下水数值模拟无法打开
      • ¥15 树型结构的数据怎么匹配分支
      • ¥15 stm32F4用.引不出来结构体成员怎么解决
      • ¥15 .exe文件打不开怎么解决?
      • ¥30 Syntax Error: TypeError: this.getOptions is not a function的问题
      • ¥15 eclipse Android环境搭建
      • ¥15 unity中脚本之间的继承有什么作用
      • ¥15 C语言内存对齐代码不太理解
      • ¥15 uniapp中的uni-file-picker组件多图上传,删除,显示。以及C#.Net后端处理问题