Go Struct Initialization with Named Literals

July 25th, 2014

Gopher Golang logo
The preferred way of initializing a struct is with named literals.

If the struct has non exported fields, and being called from other package, not using named literals will produce a compiler error like:

./main.go:65: too few values in struct initializer
In this case it’s required, but using them as a general practice can help avoid code breaking in the future if you rely on a struct that you may not control (3rd party) and may get changed in the future.

Example: