Go · Stage 7 – Structuring Data · Lesson 42 of 64

Creating and Using Structs

Create one struct value and read its fields.

Go code

type Person struct { Name string }
person := Person{Name: "Maya"}
fmt.Println(person.Name)
Line-by-line explanation
  1. A Person type is declared.
  2. The literal creates one Person.
  3. Name receives Maya.
  4. Dot notation reads the field.
Expected output
Maya

This is the expected result.