Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Linux Tipps & HardeningVAXEE NP-01 Ergo Wireless (8K) mouse thoughts(24.09.2026 um 12:38 Uhr)
Linux Tipps & HardeningQualcomm Announces Snapdragon X2 Series Processors Will Support Linux(24.09.2026 um 12:04 Uhr)
Linux Tipps & HardeningBlack Friday 2026 Phone Deals: Best iPhone, Samsung and More(24.09.2026 um 12:39 Uhr)
Linux Tipps & HardeningDont Trust Qualcomm for X2 Elite Linux Support! Liars!(24.09.2026 um 12:59 Uhr)
KI & AI VideosJulian Goldie SEO: LIVE: Building Agent OS with Claude!(24.09.2026 um 12:16 Uhr)
Linux Tipps & HardeningVAXEE NP-01 Ergo Wireless (8K) mouse thoughts(24.09.2026 um 12:38 Uhr)
Linux Tipps & HardeningQualcomm Announces Snapdragon X2 Series Processors Will Support Linux(24.09.2026 um 12:04 Uhr)
Linux Tipps & HardeningBlack Friday 2026 Phone Deals: Best iPhone, Samsung and More(24.09.2026 um 12:39 Uhr)
Linux Tipps & HardeningDont Trust Qualcomm for X2 Elite Linux Support! Liars!(24.09.2026 um 12:59 Uhr)
KI & AI VideosJulian Goldie SEO: LIVE: Building Agent OS with Claude!(24.09.2026 um 12:16 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Decrypt Go: empty struct

In Go, a normal struct typically occupies a block of memory. However, there's a special case: if it's an empty struct, its size is zero. How is this possible, and what is the use of an empty struct? This article is first published in the…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

In Go, a normal struct typically occupies a block of memory. However, there's a special case: if it's an empty struct, its size is zero. How is this possible, and what is the use of an empty struct?




This article is first published in the medium MPP plan. If you are a medium user, please follow me in medium. Thank you very much.





type Test struct {
A int
B string
}

func main() {
fmt.Println(unsafe.Sizeof(new(Test)))
fmt.Println(unsafe.Sizeof(struct{}{}))
}

/*
8
0
*/










The Secret of the Empty Struct






Special Variable: zerobase



An empty struct is a struct with no memory size. This statement is correct, but to be more precise, it actually has a special starting point: the zerobase variable. This is a uintptr global variable that occupies 8 bytes. Whenever countless struct {} variables are defined, the compiler assigns the address of this zerobase variable. In other words, in Go, any memory allocation with a size of 0 uses the same address, &zerobase.



Example




package main

import "fmt"

type emptyStruct struct {}

func main() {
a := struct{}{}
b := struct{}{}
c := emptyStruct{}

fmt.Printf("%p\n", &a)
fmt.Printf("%p\n", &b)
fmt.Printf("%p\n", &c)
}

// 0x58e360
// 0x58e360
// 0x58e360






The memory addresses of variables of an empty struct are all the same. This is because the compiler assigns &zerobase during compilation when encountering this special type of memory allocation. This logic is in the mallocgc function:




//go:linkname mallocgc  
func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
...
if size == 0 {
return unsafe.Pointer(&zerobase)
}
...






This is the secret of the Empty struct. With this special variable, we can accomplish many functionalities.






Empty Struct and Memory Alignment



Typically, if an empty struct is part of a larger struct, it doesn't occupy memory. However, there's a special case when the empty struct is the last field; it triggers memory alignment.



Example




type A struct {
x int
y string
z struct{}
}
type B struct {
x int
z struct{}
y string
}

func main() {
println(unsafe.Alignof(A{}))
println(unsafe.Alignof(B{}))
println(unsafe.Sizeof(A{}))
println(unsafe.Sizeof(B{}))
}

/**
8
8
32
24
**/







When a pointer to a field is present, the returned address may be outside the struct, potentially leading to memory leaks if the memory is not freed when the struct is released. Therefore, when an empty struct is the last field of another struct, additional memory is allocated for safety. If the empty struct is at the beginning or middle, its address is the same as the next variable.




type A struct {  
x int
y string
z struct{}
}
type B struct {
x int
z struct{}
y string
}

func main() {
a := A{}
b := B{}
fmt.Printf("%p\n", &a.y)
fmt.Printf("%p\n", &a.z)
fmt.Printf("%p\n", &b.y)
fmt.Printf("%p\n", &b.z)
}

/**
0x1400012c008
0x1400012c018
0x1400012e008
0x1400012e008
**/










Use Cases of the Empty Struct



The core reason for the existence of the empty struct struct{} is to save memory. When you need a struct but don't care about its contents, consider using an empty struct. Go's core composite structures such as map, chan, and slice can all use struct{}.






map & struct{}






// Create map
m := make(map[int]struct{})
// Assign value
m[1] = struct{}{}
// Check if key exists
_, ok := m[1]









chan & struct{}



A classic scenario combines channel and struct{}, where struct{} is often used as a signal without caring about its content. As analyzed in previous articles, the essential data structure of a channel is a management structure plus a ring buffer. The ring buffer is zero-allocated if struct{} is used as an element.



The only use of chan and struct{} together is for signal transmission since the empty struct itself cannot carry any value. Generally, it's used with no buffer channels.




// Create a signal channel
waitc := make(chan struct{})

// ...
goroutine 1:
// Send signal: push element
waitc <- struct{}{}
// Send signal: close
close(waitc)

goroutine 2:
select {
// Receive signal and perform corresponding actions
case <-waitc:
}






In this scenario, is struct{} absolutely necessary? Not really, and the memory saved is negligible. The key point is that the element value of chan is not cared about, hence struct{} is used.






Summary




  1. An empty struct is still a struct, just with a size of 0.

  2. All empty structs share the same address: the address of zerobase.

  3. We can leverage the empty struct's non-memory-occupying feature to optimize code, such as using maps to implement sets and channels.






References




  1. The empty struct, Dave Cheney

  2. Go 最细节篇— struct{} 空结构体究竟是啥?

SOC Incident Playbook: Vulnerability Remediation & Verification
title: Detect Exploitation - Decrypt Go: empty struct
id: cca2c265-7449-41c3-97a6-e97fccfa3a66
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-24
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-24"
        description = "YARA Signature for "
    strings:
        $str = "Decrypt Go: empty struct" ascii wide
    condition:
        any of them
}
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich Decrypt Go: empty struct.... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Decrypt Go: empty struct

Thematisch verwandte Begriffe: Decrypt, empty, struct · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-97152 | Nanomsg versions 0.5-beta through 1.x before 1.2.3 has a remotely exploi…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel TTP ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick