The More I Write Go - Episode IV
Go has become a very popular language in recent years. The fact that it came from Google it a big boost. The fact that you can deploy rather small, statically linked binaries, without a large runtime distributable makes it especially attractive for Docker containers and Kubernetes deployments in the cloud.
The Go language was influenced by different languages, but C is cited as the primary influencer, and yet some of the differences from C-style languages, like C, C++, C#, Java, JavaScript, do not make sense to me. Further, rather than make the syntax more lenient and user friendly, it was made more rigid and the compiler complains over every little thing.
In this first episode we will take a look at the Unary Increment and Decrement operators, ++ and --, respectively. In other C-style languages, the unary increment and decrement operators can be either statements or expressions. Further, they come in two flavors, postfix (e.g. i++), and prefix (e.g. ++i).
In Go, the Unary Increment and Decrement operators, or IncDec, as they are referred to in the Go Language Specification [1], are only available in postfix format, but that really isn't the issue as it wouldn't have made a difference if a prefix format was allowed: in Go these operators are not allowed as expressions. That means that you can only use it like so:
i++
That's it!
So the following C-style construct
arr[i++] = v
Would become
arr[i] = v
i++
And of course
arr[++i] = v
Becomes
i++
arr[i] = v
Would it kill me to add another line? Probably not. Does it feel more modern and user friendly? Absolutely not! That language "feature" is so useless that someone actually proposed to remove it in a future version [2].
And while I'm off ranting here... Hey LinkedIn! Would it kill you to add some basic code formatting to articles? It's 2018 (and even that statement would be outdated in a couple of more weeks)!
[1] https://golang.org/ref/spec#IncDec_statements
[2] https://github.com/golang/go/issues/21263
Software Architect
3 年Vedran B.