Sunday, May 19, 2024
HomeGolangWrite a operate which may recieve any sort of channel - Getting...

Write a operate which may recieve any sort of channel – Getting Assist

I wish to get a operate which may recieve any sort of chan as arguments,
(eg. chan int, chan chan int, chan myType …),

ch := make(chan int, 1)
ch2:= make(chan chan int, 2)

is there any approach?

Hello @moskong ,

A operate with a sort parameter is what you want:

func [T any](c chan T)() {}

This operate makes use of a sort parameter T that may be substituted by any sort. The operate parameter c is a chan of sort T, which matches any channel sort.

Playground hyperlink


Hello, @christophberger. Thanks to your assist, it helps me so much.
And as we speak, I get a superior drawback, is there any approach we are able to make it help any route of chan? l do this however it appears not work.

func fn[T any, C chan T | <-chan T | chan<- T](o C)

I feel a get an answer about the issue above

func fn[T any | chan T | <-chan T | chan<- T](o T)

Thanks, @moskong, for sharing your answer.

I’m not certain why this is able to be needed. The sort parameter T any is happy by any channel sort. A single sort parameter of [T any] must be ample, or am I lacking one thing?



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments