Maps

  • Set of key value pairs, hashMaps

Initialisation

var myMap map[string]uint8 = make(map[string]uint8)

  • Key is a string and value is uint8 var myMap2 = map[string]uint8{"Adam":23, "Sarah":45}
  • Map always return something when accessing a key, returning the default value of the value type
  • Fixed with the second value returned with accessing a hashmap
    • var age, ok = myMap2["jason"] where ok is true if the key exists

Methods

  • delete(myMap2, "Adam")

Loops

Maps

for name:= range myMap2 {
 
}
 
// for key value pairs
for name, age := range myMap2 {
 
}

Arrays

for i, v := range intArr {
	// i is the index and v is the value
}

While loops

  • GO does not have a while loop keyword, but it is done with the for keyword
var i int = 0
for i<10 {
	...
	i = i + 1
}
 
// OR
for {
	if i >= 10 {
		break
	}
	...
	i = i + 1
}

Traditional for loops like in Java

for i := 0; i < 10; i++ {
	...
}