user2577156
user2577156

Reputation: 33

In golang, how to return a slice of type int from a function to another function?

I have used channels to communicate..

   b := make([]int,0)  //This is the slice I have created.

I was appending values to the slice and I want to transfer the final slice stored in b to be returned to another function.. I have used this code..

   slic := make(chan int)
   go func() { slic <- input()} ()
   slice := <-slic
   fmt.Println(slice)

I am getting this error:"can't use b (type []int) as type int in return argument."

Upvotes: 1

Views: 1851

Answers (1)

Luke
Luke

Reputation: 14138

Change your chan make to this:

make(chan []int)

Or select an index of your []int to send on your chan int.

Either way int and []int are distinct types, as chan int and chan []int are.

Upvotes: 3

Related Questions