Empty Structs

An empty struct (struct{}) is a struct with no fields. Its primary feature is that it takes up zero bytes of memory.

Syntax

// Anonymous empty struct
empty := struct{}{}

// Named empty struct
type signal struct{}
s := signal{}

Why use them?

Since they consume no memory, they are perfect as “unary values” or signals. You’ll often see them used in:

  • Maps: When you only care about keys (like a set) and don’t want to waste memory on values. map[string]struct{}.
  • Channels: When you want to signal that an event occurred but don’t need to send any data.

Example: Set implementation

set := make(map[string]struct{})
set["item1"] = struct{}{} // Uses no memory for the value