go - Channel slice of integers -
i want create slice channel , contains integers.
test := make(chan []int) test <- 5
this way initialized have no idea how pass value since slices use append channels send data using <-
i have tried both <- , append , both combined below , can't work
test <- append(test, 5)
you defined channel of []int
trying send int
. have send slice of ints , have receiver use slice.
a working example here: https://play.golang.org/p/tmcuku8g-1
notice i'm appending 4 things
slice , not channel itself
package main import ( "fmt" ) func main() { c := make(chan []int) things := []int{1, 2, 3} go func() { c <- things }() _, := range <-c { fmt.println(i) } go func() { c <- append(things, 4) }() _, := range <-c { fmt.println(i) } }
output:
1 2 3 1 2 3 4
Comments
Post a Comment