#Adding a method on a struct from another package

5 messages · Page 1 of 1 (latest)

dusk herald
#

it's not possible no. You can only add methods to types you (the package) define.

sweet fulcrum
somber light
#

that's the whole point of packages, they are meant to be self contained scopes

dusk herald
#

If you want to add a method to foo.Bar you can create a new type whose underlying type is foo.Bar and add a method to that, but it's a different type.

type foo2 foo.Bar

func(foo2) f() {}```

That said, this smells like a design problem and as such I would discourage doing this and rethink your design instead
sweet fulcrum
#

understandable, thank you for the help