If you have worked with Go before, you’ve probably seen this runtime error.
The current solution is to check
thevar != nil before using var, but if you forget to do so, your program will crash. This means that this simple programmer error could shut down your entire server. Hope you catch these errors with testing before they hit production. However, we are not machines, so inevitably we will not catch some until our end users encounter a production error.
Go should have types that can never be zero
The idea is very simple and is also used by Rust and C ++: add a pointer type which can never be zero.
Jelte compares Go to Rust, saying Safe Rust doesn’t have zero capacity pointers.
However, I think comparing Go to Swift is more appropriate as Go will never get rid of null pointers.
Swift has two types of variables: Optional and non-optional. Optional means nullable.
Variables that are not optional in Swift are guaranteed to never be zero. Your function no longer needs to check its arguments for nil if you declare your non-optional arguments. Imagine removing all these duplicates
if myarg == nil in all your functions with the guarantee that your entries will never be null!
Go2 a many open proposals for non-zero types, but nothing seems decided yet. Hopefully they don’t dismiss this lightly. In my opinion, this is the only thing stopping Go from competing with Rust, Swift, TypeScript, and other languages with non-nullable types.