卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35385本站已运行395

go 通道出现问题,在封闭通道上发送

go 通道出现问题,在封闭通道上发送

php小编柚子在使用Go语言开发过程中,可能会遇到一些问题,其中之一就是在封闭的通道上发送数据时出现了问题。这个问题可能会导致通道的阻塞和程序的停滞,影响整个应用的运行。在解决这个问题之前,我们首先需要了解什么是封闭通道,以及为什么在封闭通道上发送数据会出现问题。接下来,我们将深入探讨这个问题,并提供解决方案来修复和优化我们的Go应用程序。

问题内容

我收到一个致命错误:“在关闭的通道上发送”,有时我运行此代码,我尝试了多种解决方案,但没有一个有效,这是代码的表示,易于理解且易于使用测试:

CB14CE50B218D8EAB916B15CD95527D5

我想要的是启动请求函数n次并获取第一个完成的请求,然后关闭通道并且不要向通道发送更多请求,如果没有一个请求成功完成,则等待所有goroutines完成.

我想发生这种情况是因为两个或多个 goroutine 同时检查通道是否关闭,并且两个都尝试在通道中写入,这会导致致命错误。

错误:

goroutine 19 [running]:
main.request(0xc00000a028, 0xc00000a030, 0x0?)
        C:/test/main.go:49 +0x135
created by main.main
        C:/test/main.go:17 +0xd3
panic: send on closed channel

谁能解释一下为什么会发生这种情况?

提前致谢

解决方法

问题是接收 goroutine (main) 过早关闭 outputCh。其他一些 goroutine 仍然可以尝试在其上发送。

这是另一种方法:

package main

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

func main() {
    var wg sync.WaitGroup
    output := make(chan string)
    stop := make(chan bool)
    done := make(chan bool)

    for i := 0; i < 20; i++ {
        wg.Add(1)
        go request(output, stop, &wg)
    }

    go func() {
        wg.Wait()
        done <- true
    }()

    firstOutput := <-output
    fmt.Println("output:", firstOutput)

    fmt.Println("closing the stop channel")
    close(stop)

    <-done
    fmt.Println("end of main")
}

func request(output chan string, stop chan bool, wg *sync.WaitGroup) {
    defer wg.Done()

    fmt.Println("request started")
    time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)

    select {
    case output <- "test":
        fmt.Println("output sent")
    case <-stop:
        fmt.Println("stop channel is closed")
    }
}
卓越飞翔博客
上一篇: Gorm 在自定义字符串类型上返回扫描仪错误
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏