nil value and its memory
tags: learning go programming
content
Note
for
nilstuff, NO memory is allocated, Garbage Collector does not need to manage it.
for empty stuff, memory is allocated, GC needs to track and free the memory.
- example
mySlice := new([]int) // returns a pointer to a slice structure
fmt.Printf("mem addr of mySlice: %p", &mySlice) // 0xsomeaddrB
fmt.Printf("mySlice is: %p", mySlice) // 0xsomeaddrA
fmt.Printf("value that mySlice points to: %p", *mySlice) // 0x0mySliceis a pointer to a slice, its type is*[]int- printing
mySlicereturns0x1400012e000, which is the address of the slice structure created bynew
- printing
mySliceitself is a variable, it has its own value and memory- printing
&mySlicegives the variable’s memory address
- printing
%p, &mySliceis different from%p, mySlice- the first one is the memory address of this pointer variable, the second one is the value of the pointer variable
*mySliceis a nil slice (mySlicedoesn’t point to anything), printing memory address of*mySlicegives0x0