Il giorno giovedì 26 aprile 2018 18:55:07 UTC+2, Nimrod Shneor ha scritto: > > Hey everyone, > I've encountered a design issue - > I have an the following interface - > > type Runner interface { > Run(x X,y Y) > } > > I want to add to it an Init(...) method which will initialize the internal > fields of the Runner before performing `Run`.. > My issue is that different structs implementing the `Runner` interface > require different fields in order to initialize, How should I solve this? > > You don't need an Init method. Just make sure that the instance implementing the Runner interface is initialized before calling the Run method.
As an example: package runner type Runner interface { Run(x X, y Y) } type myrunner struct { // ... } func (r *runner) Run(x X,y Y) { // ... } func New(...) Runner { r = new(runner) // initialize the runner return r } Note that the runner type is not exported, so that the user can not accidentally call the Run method before the instance is initialized. Also note that New returns Runner and not *myrunner. Here is a working example: https://play.golang.org/p/gqUiYkpq66D Manlio -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.