Golang String Fields String Syntax

I have a pretty big structure, which until I created an instance with struct literal syntax, for example:

Thing{
  "the name",
  ...
}

I just added unallocated field in the structure Thing, and now Go complains implicit assignment of unexported field 'config' in Thing literal.

Is it possible to continue using the literal syntax, although now there is an unpacked field in the structure?

+4
source share
2 answers

, , , , ( /).

, :

t := Thing{
    Name:           "the name",
    someUnexported: 23,
}

, , , :

t := otherpackage.Thing{
    Name: "the name",
    // someUnexported will implicitly be its zero value
}

, , , - ( setter), (), / .

. : ?

+4

. , :

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
}
+1

Source: https://habr.com/ru/post/1681867/


All Articles