Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang - Using chan slice inside struct

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)
}
like image 332
thomas Avatar asked Sep 10 '13 02:09

thomas


1 Answers

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.

like image 58
Gustavo Niemeyer Avatar answered Sep 30 '22 12:09

Gustavo Niemeyer