My version of effective go.
This may be going to take a long time:
func BenchmarkCheap(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
s := Expensive()
b.StartTimer()
s.Cheap()
}
}
a better way is to prepare data ahead of time:
func BenchmarkCheap(b *testing.B) {
ss := make([]S, b.N, b.N)
for i := 0; i < b.N; i++ {
ss[i] = Expensive()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ss[i].Cheap()
}
}
type myInterface interface {
shared() //same code for multiple implementations
forked() //different code
}
type delegate struct {
forkImpl func()
}
func (d *delegate) forked() {
d.forkImpl() //delegation
}
func (d *delegate) shared() {
//some reused code
}
type myImpl struct {
*delegate
}
func New() myInterface{
d := new(delegate)
d.forkImpl = func() {
//specific impl
}
return &myImpl{d}
}
Let's later see if this conflicts with "With Go-like type composition the embedded types should generally be 'whole', complete and useful on their own."