The documentation says:
We must register the concrete type for the encoder and decoder (which would
normally be on a separate machine from the encoder). On each end, this tells the
engine which concrete type is being sent that implements the interface.
So, at some point, you're going to want to call gob.Register
, but you do want your code to be maintainable. This leaves (broadly) two options:
- Creating a function like you're doing now, calling each struct after one another.
- Advantage: all your
Register
-calls in a list, so you'll easily spot if you miss one, and you surely won't register one twice.
- Disadvantage: you'll have to update it when using another implementation. You'll also have to call this function some time before encoding/decoding.
-
Creating something like this:
func Register(i interface{}) error {
gob.Register(i)
return nil
}
And then when writing a new implementation in your (let's say) dummy
package, you can put this line below / above the interface declaration.
var regResult = reg.Register(myNewInterface{})
This will be called on startup (because it's global).
- Advantage: not having to update the
registerImplementations
method.
- Disadvantage: you'll have your registers all across your code (which can consist of a lot of files) - so you might miss one.
As to which is better: I'll leave that up to you.