Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungBreeze TTS 2 vs ElevenLabs: Open Source TTS Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungAgentic AI vs Generative AI: The 2026 Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungI made my agent prove every quote against the source document(23.09.2026 um 05:45 Uhr)
Sichere Programmierung8mb.video Alternative: Skip the Line, Skip the Upsell(23.09.2026 um 05:47 Uhr)
Sichere ProgrammierungBuilding a GTA 6 JSON API for entities and current status(23.09.2026 um 05:52 Uhr)
Sichere ProgrammierungEvery filter needs a documented exception(23.09.2026 um 06:01 Uhr)
Sichere ProgrammierungBreeze TTS 2 vs ElevenLabs: Open Source TTS Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungAgentic AI vs Generative AI: The 2026 Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungI made my agent prove every quote against the source document(23.09.2026 um 05:45 Uhr)
Sichere Programmierung8mb.video Alternative: Skip the Line, Skip the Upsell(23.09.2026 um 05:47 Uhr)
Sichere ProgrammierungBuilding a GTA 6 JSON API for entities and current status(23.09.2026 um 05:52 Uhr)
Sichere ProgrammierungEvery filter needs a documented exception(23.09.2026 um 06:01 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

How To Fix Race Condition in Go: Part 2

Hello, I'm Ganesh. I'm working on FreeDevTools online, currently building a single platform for all development tools, cheat codes, and TL; DRs — a free, open-source hub where developers can quickly find and use tools without the hassle of …

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

Hello, I'm Ganesh. I'm working on FreeDevTools online, currently building a single platform for all development tools, cheat codes, and TL; DRs — a free, open-source hub where developers can quickly find and use tools without the hassle of searching the internet.



In previous part, we learned about race conditions. Now let's learn how to actually find race conditions in our code.






How Race Condition Results in Unexpected Behavior



By using few goroutines, we can see that race condition is not occurring. But when we increase the number of goroutines, we can see that race condition is occurring.



Let’s increase the number of goroutines to actualy see how race condition results:




package main

import (
"fmt"
"time"
)
func increment(counter *int) {
*counter++
}
func main() {
counter := 0
for i := 0; i < 100; i++ {
go increment(&counter)
}

time.Sleep(time.Second)
fmt.Println("Counter:", counter)
}






Expected Output should be 100



Actual Output




gk@jarvis:~/exp/code/rd$ go run main.go
Counter: 100
gk@jarvis:~/exp/code/rd$ go run main.go
Counter: 984






We used 100 goroutines to increment counter, so it should be 100.

But it’s almost always less. Each goroutine reads and writes counter at the same time, and some updates get lost.



For example:



if Goroutine A reads counter = 5, increments it to 6.

and Goroutine B reads counter = 5 (before Goroutine A writes), increments it to 6.

Both write back 6, losing one increment.



This overlapping is the main root cause of a race condition.





Detecting Race Conditions



Go has a built-in tool which helps to spot race conditions.



Run the program with the -race flag:




go run -race main.go






Let’s use last example:




package main
import (
"fmt"
"time"
)
func increment(counter *int) {
*counter++
}
func main() {
counter := 0
for i := 0; i < 100; i++ {
go increment(&counter)
}
time.Sleep(time.Second)
fmt.Println("Counter:", counter)
}






When you run go run -race main.go, you’ll see a warning like:




==================
WARNING: DATA RACE
Read at 0x00c00011e018 by goroutine 8:
main.increment()
/home/gk/exp/code/rd/main.go:9 +0x35
main.main.gowrap2()
/home/gk/exp/code/rd/main.go:15 +0x17

Previous write at 0x00c00011e018 by goroutine 7:
main.increment()
/home/gk/exp/code/rd/main.go:9 +0x47
main.main.gowrap1()
/home/gk/exp/code/rd/main.go:14 +0x17

Goroutine 8 (running) created at:
main.main()
/home/gk/exp/code/rd/main.go:15 +0x110

Goroutine 7 (finished) created at:
main.main()
/home/gk/exp/code/rd/main.go:14 +0xa6
==================
==================
WARNING: DATA RACE
Read at 0x00c00011e018 by main goroutine:
main.main()
/home/gk/exp/code/rd/main.go:18 +0x152

Previous write at 0x00c00011e018 by goroutine 8:
main.increment()
/home/gk/exp/code/rd/main.go:9 +0x47
main.main.gowrap2()
/home/gk/exp/code/rd/main.go:15 +0x17

Goroutine 8 (finished) created at:
main.main()
/home/gk/exp/code/rd/main.go:15 +0x110
==================
Counter: 2
Found 2 data race(s)
exit status 66






This tells us two goroutines are clashing over counter.



The race detector doesn’t fix the problem but helps us to find it.






Conclusion



This is very common race condition happend in any programming language but we must understand why it is happens and how to indentify it and fix it.



If you have build very large application, you can use race detector to find race conditions.



In next part, we will learn how to fix race conditions.





FreeDevTools



I’ve been building for FreeDevTools.



A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction when searching for tools and materials.



Any feedback or contributions are welcome!



It’s online, open-source, and ready for anyone to use.



👉 Check it out: FreeDevTools


⭐ Star it on GitHub: freedevtools

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How To Fix Race Condition in Go: Part 2

Thematisch verwandte Begriffe: Race, Condition, Part · 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-18163 | IBM Financial Transaction Manager (FTM) for RedHat OpenShift could allow…
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 ⏱️ 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