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

Maps in Go

Maps in Go

Luca Sepe

May 30, 2020
Tweet

More Decks by Luca Sepe

Other Decks in Programming

Transcript

  1. Day #7 - Maps A simple introduction to Maps in

    Go 「 Trying to make you to fall in love with Golang 」 1 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  2. What are Maps? A map is an unordered collection of

    key- value pairs. keys are unique while the values may not used for fast lookups, retrieval, and deletion of data based on keys In Go a map is declared using the following syntax: var m map[keyType]valType Try It 「Trying to make you to fall in love with Golang」 - Maps 2 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  3. Key types Map keys may be of any type that

    is comparable. boolean, numeric, string, pointer channel and interface types structs or arrays that contain only those types 「Trying to make you to fall in love with Golang」 - Maps 3 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  4. Initializing a Map using the the built-in make function var

    m = make(map[string]int) using a map literal: var m = map[string]float64{ "Margherita": 5.00, "Calzone": 7.50, "Diavola": 6.50, // <- Mind this comma! } Try It! 「Trying to make you to fall in love with Golang」 - Maps 4 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  5. Getting Values We can get the value assigned to a

    key using the syntax: m[key] if the key does not exists we will get the zero value of value type How to check if a key exists? Use the following two-value assignment val, ok := m[key] the boolean variable ok will be true if the key exists Try It! 5 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  6. Iterating We can iterate over a map using the for

    range loop the iteration order is undefined, non-deterministic and non-reproducible Deleting an item Use the built-in delete function does not return any value if the key does not exist it does not do anything Try It! 6 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  7. How to iterate with "order"? If a stable iteration order

    is a requirement we must maintain a separate data structure that specifies that order Try It! 「Trying to make you to fall in love with Golang」 - Maps 7 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  8. Maps gotchas Maps are just pointers to runtime types a

    map is just a pointer to a runtime.hmap struct So pay attention when you assign the same map to others variables they both refer to the same underlying data structure Try It! 8 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  9. 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」 - Maps 9 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/