-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?

Flimzy
  • 68,325
  • 15
  • 126
  • 165
Eric L.
  • 87
  • 1
  • 9

1 Answers1

4

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
  • 4,897
  • 1
  • 19
  • 25