Go

Empty Struct Sentinel (struct{})

admin by @admin ADMIN
1h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`struct{}` is a zero-size value — no memory cost. Use it as the value type in a Set (`map[T]struct{}`), as a channel signal (`chan struct{}`), or as a marker type.
Go
Raw
package main

import "fmt"

func main() {
    // Set of strings — value type is struct{}, takes ZERO bytes per entry
    seen := make(map[string]struct{})
    seen["alice"] = struct{}{}
    seen["bob"]   = struct{}{}

    // Lookup
    if _, ok := seen["alice"]; ok {
        fmt.Println("alice was seen")
    }

    // Compare to map[string]bool — bool takes 1 byte per entry; struct{} 0
    fmt.Println(len(seen))                       // 2

    // Signal channel — no data, just "something happened"
    done := make(chan struct{})
    go func() {
        // ... work ...
        done <- struct{}{}
    }()
    <-done
    fmt.Println("done received")
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.