What does this code snippet do?
var i int
_ = i
I understand the use of "_" as a blank identifier, but what does the second line in the above achieve?
Here is an example from the etcd GitHub repository: etcd
What does this code snippet do?
var i int
_ = i
I understand the use of "_" as a blank identifier, but what does the second line in the above achieve?
Here is an example from the etcd GitHub repository: etcd
The code is machine generated. The generator added the statements _ = i
to avoid unused variable declarations in the case where there's nothing to marshal.
The author of the code generator probably found it easier to add the blank assignment statements than to omit the variables when not needed.
I would guess you might do this to stop go complaining about an unused variable
It would be better to not declare the variable at all
Note, sometimes the underscore is used in imports so that the init()
code of a package will get executed, however there is no need to call functions in that package.
This is often a technique applied for image processing to register image handlers.
See A use case for importing with blank identifier in golang