Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Structs types in Go

Structs types in Go

Luca Sepe

May 23, 2020
Tweet

More Decks by Luca Sepe

Other Decks in Programming

Transcript

  1. 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/
  2. 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/
  3. 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/
  4. 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/
  5. 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/
  6. 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/
  7. 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/
  8. 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/
  9. 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/
  10. 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/
  11. 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/