Let's say I have a struct implementing an interface like below:
type IFace interface {
Method1()
Method2()
Method3()
}
type Face struct {
Prop1 string
Prop2 int
}
// IFace implementation here...
Now if I have method that accepts IFace
is it better to design it to accept a pointer to that interface of value?
func DummyMethod(f *IFace) {
(*f).Method1()
}
func DummyMethod(f IFace){
f.Method1()
}
My first guess is since these are structs, probably it's better to pass by value? Or is there a rule of thumb considering the size and nature of the struct when to start passing a pointer?
Also, when we are adding methods to a struct is it better to pass a pointer to the struct or it's value?
When passing interface type as a parameter, pass it by value, note that interface type itself would be a pointer to concrete type.
When it comes to the performance side, using interface comes with the price too, it simply cannot be inlined.
I guess it is fine to use interface with dozen calls per request/entry point, but if an app has to make thousands+ invocations, benchmark your code first before making a call.