Slide 1

Slide 1 text

Day #3 - Structs Structs types in Go 「 Trying to make you to fall in love with Golang 」 1 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 2

Slide 2 text

Defining a struct type A struct is a user-defined type that contains a collection of named fields. is a schema containing the blueprint of the data that the structure will hold 2 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 3

Slide 3 text

Declaring a variable of a named struct The zero value of a struct is a struct with all fields set to their own zero values. Try It! 「Trying to make you to fall in love with Golang」 - Structs 3 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 4

Slide 4 text

Initializing a struct by supplying all values you need to pass the field values in the same order in which they are declared in the struct you can’t initialize only a subset of fields with this syntax Try It! 「Trying to make you to fall in love with Golang」 - Structs 4 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 5

Slide 5 text

Initializing a struct by supplying fields and values in this case the order of fields is irrelevant if you want, with this syntax, you can initialize even a subset of fields only Try It! 「Trying to make you to fall in love with Golang」 - Structs 5 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 6

Slide 6 text

Accessing struct fields You can access individual fields of a struct using the dot ( . ) operator. Try It! 「Trying to make you to fall in love with Golang」 - Structs 6 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 7

Slide 7 text

Anonymous struct Structs declared and initialized without creating a new data type. great for unmarshalling JSON data in HTTP handlers If a struct is only meant to be used once, then it makes sense to declare it as anonymous. Try It! 「Trying to make you to fall in love with Golang」 - Structs 7 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 8

Slide 8 text

Anonymous fields It is possible to define a struct type without declaring any field names. by default the name of an anonymous field is the name of its type we are allowed to mix anonymous fields with named fields Try It! 「Trying to make you to fall in love with Golang」 - Structs 8 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 9

Slide 9 text

Nested structs A struct can have a field that holds another struct. you can access (or update) fields of a nested struct using the ( . ) dot notation as usual Try It! 「Trying to make you to fall in love with Golang」 - Structs 9 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 10

Slide 10 text

Promoted fields Anonymous fields of nested structs are automatically available on parent struct - that's what is called field promotion. the anonymous field Location has been promoted to parent struct User we can access this field as if it were defined on the User struct Try It! 10 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 11

Slide 11 text

Thank you Luca Sepe Solution Architect (@day) / Software Craftsman (@night) {twitter.com, github.com, linkedin.com} / lucasepe 「Trying to make you to fall in love with Golang」 - Structs 11 / 11 Structs Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/