Invalid receiver type [] T ([] T is an unnamed type) workaround?

I would like to define a method on []T , where T is the type I defined.
It seems like I should define a new type for this, but this prevents me from using all the built-in functions for slicing this new type (e.g. len ).

Is there a way to do this to just do regular functions, not methods? (It seems like append() may be a method, but not?)

+6
source share
1 answer

You can determine the type of slice:

 type MySliceType []SomeType 
  • You can still use the operations to add and cut MySliceType values.
  • You can define methods on MySliceType .

However, you cannot use monkeypatch []SomeType .

+10
source

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


All Articles