DEV Community

Cover image for đź§©Golang Type Memory Representation Quick Reference
arshia_rgh
arshia_rgh

Posted on

đź§©Golang Type Memory Representation Quick Reference

This article may be updated and improved later

This article provides a quick reference for Go types and their actual memory representation. Instead of focusing on syntax or usage, it explains what happens under the hood when you create a value of each type — how it is stored in memory, its internal structure, and its size on 32-bit and 64-bit architectures.


Pointer

  • What is: Memory address pointing to another value.
unsafe.Pointer
*int, *string ...
Enter fullscreen mode Exit fullscreen mode
  • Memory:
    • 64-bit -> 8 bytes
    • 32-bit -> 4 bytes
  • Note: A pointer can be nil, meaning it points to nothing.

String

  • What is: Pointer to its underlying Bytes + len
type StringHeader struct {
    Data uintptr
    Len  int
}
Enter fullscreen mode Exit fullscreen mode
  • Memory: A pointer + int
    • 64-bit -> 8 + 8 = 16 bytes
    • 32-bit -> 4 + 4 = 8 bytes

Int, Float, complex(real + imaginary floats) or uint

If a numeric type is not explicitly sized (for example int or uint), its size depends on the architecture — 32-bit or 64-bit.

If the type is explicitly sized (int8, int64, complex64, etc.), its memory size matches the type name.

For example:

  • int → 4 bytes on 32-bit, 8 bytes on 64-bit

  • complex64 → 8 bytes (two float32 values)


Slice

  • What is: Pointer to its underlying Bytes + len + cap
type SliceHeader struct {
    Data uintptr
    Len  int
    Cap  int
}
Enter fullscreen mode Exit fullscreen mode
  • Memory:
    • 64-bit -> 8 + 8 + 8 = 24 bytes
    • 32-bit -> 4 + 4 + 4 = 12 bytes
  • Note: Nillable. A slice is nil when its data pointer is nil and both len and cap are zero.

Map

  • What is: Pointer to the struct hmap(no_swiss) (maps.Map for the swiss)
  • Memory:
    • 64-bit -> 8 bytes
    • 32-bit -> 4 bytes
  • Note: Nillable (It is a Pointer)

Chan

  • What is: Pointer to the struct hchan
  • Memory:
    • 64-bit -> 8 bytes
    • 32-bit -> 4 bytes
  • Note: Nillable (It is a Pointer)

Struct

  • What is: Inline layout of its fields. If it has no fields, it occupies zero bytes.
  • Memory: Sum of field sizes + alignment padding. which are 8 bytes or 4 bytes ( based on the architecture)
type Example struct {
    A int8   // 1 byte
    B int32  // 4 bytes
}
Enter fullscreen mode Exit fullscreen mode

In the above example, the total size will be 8 bytes (on 64-bit) due to alignment padding.

type Example2 struct {
    A int64  // 8 bytes
    B bool   // 1 Bytes
}
Enter fullscreen mode Exit fullscreen mode

Example2 will occupy 16 bytes on a 64-bit system.


Interface

  • What is: Two Pointers(Type info pointer + data pointer)
type EmptyInterface struct {
    Type *Type
    Data unsafe.Pointer
}

type NonEmptyInterface struct {
    ITab *ITab
    Data unsafe.Pointer
}

Enter fullscreen mode Exit fullscreen mode
  • Memory:

    • 64-bit -> 8 + 8 = 16 Bytes
    • 32-bit -> 4 + 4 = 8 Bytes
  • Note: Nillable. An interface is nil when both its type and data pointers are nil.


Func

  • What is: Pointer to the function's code in memory.
  • Memory:
    • 64-bit -> 8 Bytes
    • 32-bit -> 4 Bytes
  • Note: Nillable, (It is a Pointer).

Category Type Size (64-bit) Size (32-bit) Note
Boolean bool 1 byte 1 byte Represents true or false. May include padding in structs.
Integer (Signed) int8 1 1 8-bit signed integer.
int16 2 2 16-bit signed integer.
int32 4 4 32-bit signed integer.
int64 8 8 64-bit signed integer.
int 8 4 Machine word-sized integer (depends on architecture).
Integer (Unsigned) uint8 1 1 8-bit unsigned integer (alias: byte).
uint16 2 2 16-bit unsigned integer.
uint32 4 4 32-bit unsigned integer.
uint64 8 8 64-bit unsigned integer.
uint 8 4 Machine word-sized unsigned integer.
uintptr 8 4 Unsigned integer large enough to store a pointer.
Text Types string 16 8 Struct: pointer to data + length (Data uintptr, Len int). Immutable.
rune 4 4 Alias for int32, represents a Unicode code point.
byte 1 1 Alias for uint8, represents a raw byte.
Floating Point float32 4 4 32-bit IEEE 754 floating-point number.
float64 8 8 64-bit IEEE 754 floating-point number.
Complex Numbers complex64 8 8 Two float32 values: real + imaginary (4 + 4).
complex128 16 16 Two float64 values: real + imaginary (8 + 8).
Composite Types array varies varies Fixed-length, contiguous block of elements. Value type.
slice 24 12 Struct: pointer + length + capacity (Data uintptr, Len int, Cap int). Reference type.
struct varies varies Inline fields + alignment padding. Value type.
map 8 4 Pointer to runtime.hmap (hash table). Reference type.
chan 8 4 Pointer to runtime.hchan (channel buffer & state). Reference type.
interface 16 8 Struct: type info pointer + data pointer (itab*, data unsafe.Pointer). Reference type.
pointer (*T) 8 4 Direct memory address pointing to value of type T.
function 8 4 Pointer to it's code.
Special Types nil — — Zero value for reference types. No memory allocation.
Unsafe / Runtime unsafe.Pointer 8 4 Untyped pointer; can convert to/from uintptr.
Empty Struct struct{} 0 0 Takes no space; used for signals or markers.

Top comments (0)