Simply running fmt.Println(unsafe.Sizeof("")) prints 16. Changing the content of the string doesn't affect the outcome.
Can someone explain how where this number (16) come from?
Simply running fmt.Println(unsafe.Sizeof("")) prints 16. Changing the content of the string doesn't affect the outcome.
Can someone explain how where this number (16) come from?
Strings in Go are represented by reflect.StringHeader containing a pointer to actual string data and a length of string:
type StringHeader struct {
Data uintptr
Len int
}
unsafe.Sizeof(s) will only return the size of StringHeader struct but not the pointed data itself. So (in your example) it will be sum of 8 bytes for Data and 8 bytes for Len making it 16 bytes.