1

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Eric L.
  • 187
  • 1
  • 3
  • 10

1 Answers1

7

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.

blami
  • 6,588
  • 2
  • 23
  • 31