Hacker News new | past | comments | ask | show | jobs | submit login
Why Golang slices still surprise me – Blog – build-your-own.org (build-your-own.org)
4 points by rbanffy 78 days ago | hide | past | favorite | 2 comments



This merging of semantically different concepts into the same language feature was the biggest mistake of Go in my opinion. The gains in simplicity are entirely offset by the losses in correctness and comprehension.

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.


> The worst example is pointers since they're used to represent both references and optional values.

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
    }




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: