Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Why Golang slices still surprise me – Blog – build-your-own.org (build-your-own.org)
4 points by rbanffy on Dec 3, 2024 | 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
    }




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

Search: