Slide 1

Slide 1 text

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/

Slide 2

Slide 2 text

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/

Slide 3

Slide 3 text

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/

Slide 4

Slide 4 text

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/

Slide 5

Slide 5 text

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/

Slide 6

Slide 6 text

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/

Slide 7

Slide 7 text

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/

Slide 8

Slide 8 text

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/

Slide 9

Slide 9 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」 - Maps 9 / 9 Maps Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/