little things
It’s hard to answer the question “What makes go a better programming language?” Most people jump to the headline feature of concurrency, but for me it’s the little things that are done right. Smart defaults, not too many special cases.
A couple weeks ago I slacked a friend this sample code:
switch true {
case blahblahblah:
...
case foofoofoo:
...
default:
...
}
The point being that I had just realized that it was possible to (re)use the switch statement as a replacement for the common if ... else if ... else if ... else ...
construct.
We both sort of concluded that this was kinda ugly and it’s probably preferable to just do if ... else ...
. Having true
(a constant) as the “variable” in a switch just seemed a little off.
Then yesterday I learned that go officially supports switches without a value to switch on. In such cases, the value is implicitly true
. Doh.
So my example becomes
switch {
case blahblahblah:
...
case foofoofoo:
...
default:
...
}
This, to me, is an example of the go designers doing little things to make the language better, without having to make a lot of noise about it.
So, if you prefer switch
to if ... else
for big blocks of multiple branches, switch
is an officially sanctioned way to do that.