登录
首页 >  Golang >  Go问答

在 Go 语言中同时使用两个 fmt.Println 会导致死锁吗?

来源:stackoverflow

时间:2024-02-18 16:12:28 179浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《在 Go 语言中同时使用两个 fmt.Println 会导致死锁吗?》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我正在尝试学习 go,并且正在操场上进行实验。我有一个非常简单的代码。我试图在 go 例程中一起使用结构和切片。我不确定这是否会是我在生产中使用的东西,但它似乎有点不对劲,所以这里:

func main() {
    routinemsg := make(chan []Person)
    routinemsg2 := make(chan []Person)

    // create the person records
    p1 := newPerson("john doe", 25)
    p2 := newPerson("dohn joe", 52)
    p3 := newPerson("bohn joo", 30)

    // send a slice of Person to the first routine
    go func() { routinemsg <- []Person{p1, p2} }()

    // retrieve the slice from the first routine[in the append]
    // append p3 to the slice retrieved from the first routine
    // send the new slice to the second routine
    go func() { routinemsg2 <- append(<-routinemsg, p3) }()
    
    // I am able to see the first Println but when I insert the second one I get a deadlock error
    // also, same error if I use one Println with 2 arguments.
    fmt.Println(<-routinemsg)
    fmt.Println(<-routinemsg2)
}

我听说过等待组,但还不了解它们!所以,对我好一点:d,感谢您抽出时间


正确答案


routinemsg 上只有一个发送操作,但您有 2 个接收操作:一个在启动的 goroutine 中,另一个在 main goroutine 中。发送的值只能由一个接收者接收一次。

如果启动的goroutine先从routinemsg接收,那么就会出现死锁:main中的接收将永远阻塞。

如果 main goroutine 首先接收,那么启动的 goroutine 将永远阻塞(尝试从中接收),因此它永远无法在 routinemsg2 上发送任何内容,因此 main 中从 routinemsg2 接收也将永远阻塞:再次死锁。

删除 main() 中的 fmt.println(<-routinemsg) 行,然后从 routinemsg2 的最终接收可以(最终)继续并打印包含 p1p2 和 phpcnc 的切片phpcnp3:

[{john doe 25} {dohn joe 52} {bohn joo 30}]

go playground 上尝试一下。

好了,本文到此结束,带大家了解了《在 Go 语言中同时使用两个 fmt.Println 会导致死锁吗?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>