maps
This commit is contained in:
31
about_maps.go
Normal file
31
about_maps.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package go_koans
|
||||
|
||||
func testMaps() {
|
||||
ages := map[string]int{
|
||||
"bob": 10,
|
||||
"joe": 20,
|
||||
"dan": 30,
|
||||
}
|
||||
|
||||
age := ages["bob"]
|
||||
assert(age == __int__) // map syntax is warmly familiar
|
||||
|
||||
age, ok := ages["bob"]
|
||||
assert(ok == __bool__) // with a handy addition for presence checking
|
||||
|
||||
age, ok = ages["steven"]
|
||||
assert(age == __int__) // the zero value is used when absent
|
||||
assert(__bool__) // though there are better ways to check for presence
|
||||
|
||||
assert(len(ages) == __int__) // length is based on keys
|
||||
|
||||
ages["bob"] = 99
|
||||
assert(ages["bob"] == __int__) // values can be changed for keys
|
||||
|
||||
ages["steven"] = 77
|
||||
assert(ages[__string__] == 77) // new ones can be added
|
||||
|
||||
delete(ages, "steven")
|
||||
age, ok = ages["steven"]
|
||||
assert(__bool__) // key/value pairs can be removed
|
||||
}
|
||||
@@ -26,10 +26,10 @@ func TestKoans(t *testing.T) {
|
||||
//testAnonymousFunctions()
|
||||
//testVariadicFunctions()
|
||||
//testFiles()
|
||||
testInterfaces()
|
||||
//testInterfaces()
|
||||
testMaps()
|
||||
|
||||
// TODO: ie, gameplan
|
||||
//testMaps()
|
||||
//testStructs()
|
||||
//testAllocation()
|
||||
//testPointers()
|
||||
|
||||
Reference in New Issue
Block a user