Let's say I have a struct with another struct embedded in it.
type Base struct { ID string } type Child struct { Base a int b int }
When I go to initialize Child
, I can't initialize the ID
field directly.
// unknown field 'ID' in struct literal of type Child child := Child{ ID: id, a: a, b: b }
I instead have to initialize the ID field separately.
child := Child{ a: 23, b: 42 } child.ID = "foo"
This would seem to violate encapsulation. The user of Child has to know there's something different about the ID field. If I later moved a public field into an embedded struct, that might break initialization.
I could write a NewFoo()
method for every struct and hope everyone uses that, but is there a way to use the struct literal safely with embedded structs that doesn't reveal some of the fields are embedded? Or am I applying the wrong pattern here?
2 ways to create and initialize a new structThe new keyword can be used to create a new struct. It returns a pointer to the newly created struct. You can also create and initialize a struct with a struct literal. An element list that contains keys does not need to have an element for each struct field.
Initializing nested Structuresstruct person { char name[20]; int age; char dob[10]; }; struct student { struct person info; int rollno; float marks[10]; } struct student student_1 = { {"Adam", 25, 1990}, 101, 90 }; The following program demonstrates how we can use nested structures.
Using designated initializers, a C99 feature which allows you to name members to be initialized, structure members can be initialized in any order, and any (single) member of a union can be initialized.
Creating and initializing a Struct in GoLang 1 Using struct Literal Syntax Struct literal syntax is just assigning values when declaring and it is really easy. ... 2 Using the new keyword We can use the new keyword when declaring a struct. Then we can assign values using dot notation to initialize it. ... 3 Using pointer address operator
Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type. Structs are the only way to create concrete user-defined types in Golang. Struct types are declared by composing a fixed set of unique fields.
To define a new struct type, you list the names and types of each field. The default zero value of a struct has all its fields zeroed. You can access individual fields with dot notation. The new keyword can be used to create a new struct. It returns a pointer to the newly created struct.
Before we declare and initialize a variable of a struct type, we need first to define a struct type with fields or properties. We can do it using the type keyword as follows. What does the Person struct definition tell us? It tells us that the Person struct can hold two strings and an integer value.
Go issue 9859 proposes a change the the language to support the Child{ ID: id, a: a, b: b }
syntax from the question.
Use nested composite literals to initialize a value in a single expression:
child := Child{Base: Base{ID: id}, a: a, b: b}
It is not possible to hide the fact that a field is promoted from an embedded struct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With