diff --git a/about_maps.go b/about_maps.go new file mode 100644 index 0000000..2d921fc --- /dev/null +++ b/about_maps.go @@ -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 +} diff --git a/setup_koans_test.go b/setup_koans_test.go index 5b1e977..28c6db9 100644 --- a/setup_koans_test.go +++ b/setup_koans_test.go @@ -26,10 +26,10 @@ func TestKoans(t *testing.T) { //testAnonymousFunctions() //testVariadicFunctions() //testFiles() - testInterfaces() + //testInterfaces() + testMaps() // TODO: ie, gameplan - //testMaps() //testStructs() //testAllocation() //testPointers()