Wow, yeah. This is pretty bad. The example doesn't even make logical sense. Why is ByLength a type? Shouldn't you just want to pass in an array?
Can any "idiomatic" Go programmers here give a better example of how you would do this?
Can any "idiomatic" Go programmers here give a better example of how you would do this?
Sure.
sort.Sort(sort.Reverse(sort.StringSlice(vs)));
type StringSliceByLength struct {
sort.StringSlice;
}
func (s StringSliceByLength) LessThan(i, j int) bool {
return len(s.StringSlice[i]) < len(s.StringSlice[j]);
}
Sort.sort(StringSliceByLength(vs))Instead of methods, you could of course write normal functions taking []string, but only methods can fulfil interfaces.