Files
go-koans/about_allocation.go
2012-03-16 10:43:33 -05:00

24 lines
599 B
Go

package go_koans
func aboutAllocation() {
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
}