Why declare like “var _ = func” in golang?

When I read a copy of the coreos/etcd source code, I find there are variables declared which make me quite confused. The code is:

var _ codes.Code
var _ io.Reader
var _ status.Status
var _ = runtime.String
var _ = utilities.NewDoubleArray

The file is auto generated. The packages in question might or might not be used depending on the generated code. To avoid having to understand that case in the code generation, these are added so that the packages are always “used”. This avoids a compiler error.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.