Why Golang slices still surprise me – Blog – build-your-own.org
build-your-own.org
build-your-own.org
The worst example is pointers since they're used to represent both references and optional values. It's often very difficult to know which meaning a function is using at first glance.
using a pointer as an optional value is an anti-pattern. correct idiom is to return a tuple:
type positive struct{}
func new_positive(i int) (*positive, bool) {
if i >= 0 {
return positive{}, true
}
return nil, false
}