Golang - Using chan slice inside struct

喜夏-厌秋 提交于 2019-12-10 14:57:28

问题


I am trying to use a slice chan type inside a struct, similar to the code below. However, when I try to receive at

test := <-c.slice

The program hangs. Is there a way to do this?

package main

import "fmt"

type blah struct {
    slice chan [][]int
}

func main() {

    slice := make([][]int, 3)
    c := blah{make(chan [][]int)}

    slice[0] = []int{1, 2, 3}
    slice[1] = []int{4, 5, 6}
    slice[2] = []int{7, 8, 9}

    go func() {
        test := <- c.slice
        test = slice
        c.slice <- test
    }()

    fmt.Println(<-c.slice)
}

回答1:


The first line inside the goroutine receives from the channel, and so does the first line in main after the goroutine is created. That means the only two goroutines in the system are both trying to receive from the slice channel, and nobody is trying to send into it.

More generally, the issue is that for somebody to receive (consume), somebody else has to concurrently send (produce). If the channel was buffered, which isn't the case in your example, the send might also happen ahead of time in the same goroutine.

So this works, for example, because the channel has a buffer of one element, which means the send won't block:

ch := make(chan int, 1)
ch <- 1
i := <-ch

This works as well, because the send is happening concurrently with the receive:

ch := make(chan int)
go func() { ch <- 1 }()
i := <-ch

This does not work, because both goroutines are trying to send into an unbuffered channel, and nobody is trying to receive:

ch := make(chan int)
go func() { ch <- 1 }()
ch <- 2

This does not work either, because both goroutines are trying to receive, and nobody is trying to send:

ch := make(chan int)
go func() { <-ch }()
<-ch

That last one is your case.



来源:https://stackoverflow.com/questions/18710010/golang-using-chan-slice-inside-struct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!