diff --git a/about_allocation.go b/about_allocation.go new file mode 100644 index 0000000..73199f5 --- /dev/null +++ b/about_allocation.go @@ -0,0 +1,23 @@ +package go_koans + +func testAllocation() { + a := new(int) + *a = 3 + assert(*a == __int__) // new() creates a pointer to the given type, like malloc() in C + + type person struct { + name string + age int + } + bob := new(person) + assert(bob.age == __int__) // it can allocate memory for custom types as well + + slice := make([]int, 3) + assert(len(slice) == __int__) // make() creates slices of a given length + + slice = make([]int, 3) + assert(cap(slice) == 20) // but can also take an optional capacity + + m := make(map[int]string) + assert(len(m) == __int__) // make() also creates maps +} diff --git a/setup_koans_test.go b/setup_koans_test.go index 7081d88..d210720 100644 --- a/setup_koans_test.go +++ b/setup_koans_test.go @@ -29,10 +29,10 @@ func TestKoans(t *testing.T) { //testInterfaces() //testMaps() //testPointers() - testStructs() + //testStructs() + testAllocation() // TODO: ie, gameplan - //testAllocation() //testGoroutines() //testChannels() //testPanics()