Insert channel into structure

How to insert a channel into a structure in Go ?

Why inconsistency between map syntax:

var m map[string]int 

and channel

 var m chan int 

?

To clarify, in Go you can insert a type into another type. An inline type gains access to all methods defined in the inline type, but you can also explicitly refer to the inline type by its type name. Therefore, the inconsistency between the map type declaration and the channel type declaration is confused for those who would like to refer to the type of the embedded channel.

+1
source share
1 answer

The problem is that embedding allows you to mostly use built-in type methods (as indicated in the Attachment instead of inheritance in Go ")

And channel , such as map , is an unnamed type (specified using a type literal that represents a new type from existing types.).
It does not have its own or exported fields, so you don’t go very far by introducing the channel type within struct {} .

You probably have an error message similar to the example :

 func (x chan int) m2() {} invalid receiver type chan int (chan int is an unnamed type) 

If the channel type is embedded in the struct type, this unnamed type will act as a receiver for the methods , which, apparently, is not allowed by the language in the first place.

+9
source

Source: https://habr.com/ru/post/1205003/


All Articles