Skip to content

proposal: Go 2: permit conversion from []chan int to []<-chan int #41695

Closed
@scorsi

Description

@scorsi

I would like to find a solution to #40010.

To remind the problem is:

func myFunc() []<-chan int {
	var res []chan int
	return res
}

Which result on a Compile error: cannot use res (type []chan x) as type []<-chan x in return argument.

The only workaround is either

  • To return a slice of channels without receive-only or send-only type qualifier.
    But it's not dev-friendly, what the dev have to do with a simple chan ? read or write ?.. This is what I choose actually.
  • Or to append the channels to a new slice with the right type.
func myFunc() []<-chan int {
	var chans []chan int
        // do your stuff to chans
        var res []<-chan int
        for c, _ := range chans {
                res = append(res, c)
        }
	return res
}

Which is very compute-time consuming and not the right solution too.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions