Rework Maps Section
jcbwlkr opened this issue · 2 comments
@ardan-bkennedy I want to take the existing map examples, break them apart, and reorder the content. I get the feeling that when this material was written it was assumed that the student was already familiar with maps and we are just covering them to nail down some of the "gotchas" as well as the best practices. Consider that the first example right off the bat is jumping into iterating over a map and how it is in an undefined order. If you are just learning maps this is a confusing place to start.
Here is how I want to teach the materials at least for Go Fundamentals. Do you think this would work with Ultimate? It's still basically the same content (though I expand the coverage of zero value for non-present keys)
Example 1: Declare, write, read, and delete
Start with the basics.
- Create a map with make:
users := make(map[string]user)
- Set individual values in the map.
users["Roy"] = user{"Rob", "Roy"}
- Read individual values from the map.
roy := users["Roy"]
- Delete values from the map.
delete(users["Roy"])
Example 2: Reading a non-present key
We just talked about delete so it seems natural to move on to this.
- Reading a non-present key gives zero value.
u := users["Roy"]
- Use two-assignment form if you need to know.
u, found := users["Roy"]
- Talk about how we can conveniently write
scores["anna"]++
without checking for existence.
Example 3: Map key type restrictions
This could be last after the range stuff? It just needs to be covered before we're done.
- Explain how/why a map's key type must be "comparable". Can't be a slice, map, or func.
Example 4: Map literal and range
- Declare a map with predefined values
- Range over it twice. Show how order is pseudo-random.
Example 5: Ranging in a specific order
- Show the existing example4.go which covers one way to iterate a map in a defined order.
We should also have examples showing that you cant take the address of an element in a map.
I don't teach maps so go with your gut on this!!