Search code examples
pointersgostructslicecomposite-literals

Slice of structs behaviour


When we declare a slice of structs, but set the data type to a pointer to the struct, does go automatically convert the literals to pointers?

type book struct {
    id int
    name string
}

var books = []*book {
    {1, "Yellow moon"}, // ---> Is this a pointer to book OR just book
}

Solution

  • It is possible to define with and without type:

    https://play.golang.org/p/CB2MDPDztrc