Unused Code in Go (Golang)
In the Go language (Golang) there is a concept that threw me for at first. In other languages, you can have unused code sitting around. By unused I don’t mean comments, instead I mean code that isn’t actively being consumed by the application. For example, if I declared two variables (a and b) but only initialized variable a, even though the variable assignment for b is valid – it will not compile, as variable b is not getting consumed.
This language requirement keeps the code useful.
Blank Identifiers in Go (Golang)
The underscore is the blank identifier. It is used to stand in place of a variable assignment. For example, if you had a call to http.Get you’ll get back the response or an error. Instead of writing the catch code to handle an error, you could call:
resp, _ : = http.Get(….)
Comments are closed