Struct in Go

Declare a struct:

1
2
3
4
5
6
7
8
9
type person struct {
    name, lastname   string
    age              int
}

var alice struct {
    name, lastname   string
    age              int
}

Struct instance:

1
2
3
4
5
6
7
8
9
picasso := person{
    name:     "Pablo",
    lastname: "Picasso",
    age:      91,
}

alice.name = "Alice"
alice.lastname = "Wonderland"
alice.age = 21

Note: Struct can be compared. You can’t compare struct values that contains incomparable fields. You need to compare them manually.

Embed struct to another struct:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
type text struct {
    title string
    words int
}

type book struct {
    // embed the text
    text
    isbn  string
}

Say b1 is instance of book stuct, then b1.title is equal to b1.text.title and b1.words is equal to b1.text.words.