0fd71ac180c7de4b0ca770a80e325f326ea4dd39 — kotrunga 2 months ago 7455a83
Add through the end of maps at 1.3.23!
M basics/structs-slices-maps/1.3.19_maps.go => basics/structs-slices-maps/1.3.19_maps.go +1 -1
@@ 1,7 1,7 @@ /*
  	Notes 1.3.19
  
- 	A map "maps" keys to values.
+ 	A map "maps" keys to values. In Golang, maps are implemented as hash tables.
  
  	The zero value of a map is nil.
  */

A basics/structs-slices-maps/1.3.20_map-literals.go => basics/structs-slices-maps/1.3.20_map-literals.go +26 -0
@@ 0,0 1,26 @@
+ /*
+ 	Notes 1.3.20
+ 
+ 	- map literals are declared like struct literals... but the keys are required
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ type Vertex struct {
+ 	Lat, Long float64
+ }
+ 
+ var m = map[string]Vertex {
+ 	"Bell Labs": Vertex {
+ 		40.68433, -74.39967,
+ 	},
+ 	"Google": Vertex {
+ 		37.42202, -122.08408,
+ 	},
+ }
+ 
+ func main() {
+ 	fmt.Println(m)
+ }< 
\ No newline at end of file

A basics/structs-slices-maps/1.3.21_map-literals-continued.go => basics/structs-slices-maps/1.3.21_map-literals-continued.go +23 -0
@@ 0,0 1,23 @@
+ /*
+ 	Notes 1.3.21
+ 
+ 	- If the top level type is just a type name, you
+ 	  can omit it from the elements of the literal
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ type Vertex struct {
+ 	Lat, Long float64
+ }
+ 
+ var m = map[string]Vertex {
+ 	"Bell Labs": { 40.68433, -74.39967 },
+ 	"Google": Vertex { 37.42202, -122.08408 },
+ }
+ 
+ func main() {
+ 	fmt.Println(m)
+ }< 
\ No newline at end of file

A basics/structs-slices-maps/1.3.22_mutating-maps.go => basics/structs-slices-maps/1.3.22_mutating-maps.go +32 -0
@@ 0,0 1,32 @@
+ /*
+ 	Notes 1.3.22
+ 
+ 	When working with map 'm'...
+ 	- Insert or update elements: m[key] = elem
+ 	- Retrieving an element: elem = m[key]
+ 	- Deleting an element: delete(m, key)
+ 	- Testing that a key is present: elem, ok = m[key]
+ 	  - If the key is in m, ok is true. If not, ok is false.
+ 	  - If key is not in the map, then elem is the zero value for the map's element type.
+ 	  - If they (elem or ok) haven't been declared yet... elem, ok := m[key]
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ func main() {
+ 	m := make(map[string]int)
+ 
+ 	m["Answer"] = 42
+ 	fmt.Println("The value:", m["Answer"])
+ 
+ 	m["Answer"] = 48
+ 	fmt.Println("The value:", m["Answer"])
+ 
+ 	delete(m, "Answer")
+ 	fmt.Println("The value:", m["Answer"])
+ 
+ 	v, ok := m["Answer"]
+ 	fmt.Println("The value:", v, "Present?", ok)
+ }< 
\ No newline at end of file

A basics/structs-slices-maps/1.3.23_exercise-maps.go => basics/structs-slices-maps/1.3.23_exercise-maps.go +38 -0
@@ 0,0 1,38 @@
+ /*
+ 	Notes 1.3.23
+ 
+ 	For this one, copy and run the code on the 'A Tour of Go' website.
+ 	This code is made for the test suite built into this exercise:
+ 	https://tour.golang.org/moretypes/23
+ 
+ 	To be more efficient, the for loop / range could just contain this in the body:
+ 	'wordCount[word]++'
+ */
+ 
+ package main
+ 
+ import (
+ 	"golang.org/x/tour/wc"
+ 	"strings"
+ )
+ 
+ func WordCount(s string) map[string]int {
+ 	words := strings.Fields(s)
+ 	wordCount := make(map[string]int)	// expanded: var wordCount map[string]int = make(map[string]int)
+ 	
+ 	for _, word := range words {
+ 		_, exists := wordCount[word]
+ 		
+ 		if exists {
+ 			wordCount[word] = wordCount[word] + 1
+ 		} else {
+ 			wordCount[word] = 1
+ 		}
+ 	}
+ 
+ 	return wordCount
+ }
+ 
+ func main() {
+ 	wc.Test(WordCount)
+ }< 
\ No newline at end of file