C# has Extension Methods , which allow you to add methods to existing types, regardless of where they are defined by you or in a third-party assembly.
Go has receiver methods , which are functions that are called on a value of a specific type. The type has to be defined within your package, and the receiver is the value that is being passed to the method.
I wanted to accomplish a similar behavior in Go.
Check out the repo here: https://github.com/ElanHasson/go-experiments-kinda-extension-methods .
I figured out how to creating a struct
of the struct
I wanted to extend.
There is one caveat here: You must implement all methods the base struct
has, even if they are empty, otherwise if you call it, you’ll get a runtime panic.
|
|
Comments