Golang struct literal syntax with unexported fields

Viewed 19020

I've got a largish struct which until just now I was instantiating with the struct literal syntax, e.g.:

Thing{
  "the name",
  ...
}

I've just added an unexported field the Thing struct and now Go is complaining: implicit assignment of unexported field 'config' in Thing literal.

Is there any way I can continue using the literal syntax even though there's now an unexported field on the struct?

2 Answers

One more point to add. All properties of a structure should start with capital letter if they are supposed to be visible outside the package. For example:

t := Thing
{

    Name: "the name", // <-- This will work because Name start with capital letter 

    someUnexported: 23, // <-- this wont work because someUnexported starts with small letter
}
Related