Learn GO with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
Go Counter with Goroutines
package main
import (
"fmt"
"sync"
)
func main() {
var count int
var mu sync.Mutex
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
mu.Lock()
count++
fmt.Println("Count:", count)
mu.Unlock()
}()
}
wg.Wait()
}
Demonstrates a simple counter updated concurrently with goroutines.
2
Go Theme Toggle
package main
import (
"fmt"
"sync"
)
func main() {
var isDark bool
var mu sync.Mutex
var wg sync.WaitGroup
toggle := func() {
mu.Lock()
isDark = !isDark
fmt.Println("Theme:", map[bool]string{true: "Dark", false: "Light"}[isDark])
mu.Unlock()
}
for i := 0; i < 3; i++ {
wg.Add(1)
go func() { defer wg.Done(); toggle() }()
}
wg.Wait()
}
Toggles a dark/light theme flag concurrently.
3
Go Score Tracker
package main
import (
"fmt"
"sync"
)
func main() {
score := 0
var mu sync.Mutex
var wg sync.WaitGroup
increment := func() {
mu.Lock()
score += 10
fmt.Println("Score:", score)
mu.Unlock()
}
decrement := func() {
mu.Lock()
score -= 5
fmt.Println("Score:", score)
mu.Unlock()
}
wg.Add(2)
go func() { defer wg.Done(); increment() }()
go func() { defer wg.Done(); decrement() }()
wg.Wait()
}
Tracks a score with concurrent increment and decrement.
4
Go Simple Timer
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
count := 0
for count < 3 {
<-ticker.C
count++
fmt.Println("Time:", count, "sec")
}
}
Counts seconds using goroutines and channels.
5
Go Health Tracker
package main
import (
"fmt"
"sync"
)
func main() {
health := 100
var mu sync.Mutex
var wg sync.WaitGroup
damage := func() {
mu.Lock()
health -= 20
fmt.Println("Health:", health)
mu.Unlock()
}
heal := func() {
mu.Lock()
health += 10
fmt.Println("Health:", health)
mu.Unlock()
}
wg.Add(2)
go func() { defer wg.Done(); damage() }()
go func() { defer wg.Done(); heal() }()
wg.Wait()
}
Tracks health with concurrent damage and healing operations.
6
Go Level Tracker
package main
import (
"fmt"
"sync"
)
func main() {
level := 1
var mu sync.Mutex
var wg sync.WaitGroup
nextLevel := func() {
mu.Lock()
level++
fmt.Println("Level:", level)
mu.Unlock()
}
wg.Add(2)
go func() { defer wg.Done(); nextLevel() }()
go func() { defer wg.Done(); nextLevel() }()
wg.Wait()
}
Tracks game levels using goroutines safely.
7
Go Coin Counter
package main
import (
"fmt"
"sync"
)
func main() {
coins := 0
var mu sync.Mutex
var wg sync.WaitGroup
collectCoin := func() { mu.Lock(); coins++; fmt.Println("Coins:", coins); mu.Unlock() }
loseCoin := func() { mu.Lock(); coins--; fmt.Println("Coins:", coins); mu.Unlock() }
wg.Add(2)
go func() { defer wg.Done(); collectCoin() }()
go func() { defer wg.Done(); loseCoin() }()
wg.Wait()
}
Counts coins collected and lost concurrently.
8
Go Ammo Tracker
package main
import (
"fmt"
"sync"
)
func main() {
ammo := 10
var mu sync.Mutex
var wg sync.WaitGroup
shoot := func() { mu.Lock(); ammo--; fmt.Println("Ammo:", ammo); mu.Unlock() }
reload := func() { mu.Lock(); ammo = 10; fmt.Println("Ammo reloaded:", ammo); mu.Unlock() }
wg.Add(2)
go func() { defer wg.Done(); shoot() }()
go func() { defer wg.Done(); reload() }()
wg.Wait()
}
Tracks ammo usage with shoot and reload actions using goroutines.
9
Go Star Collector
package main
import (
"fmt"
"sync"
)
func main() {
stars := 0
var mu sync.Mutex
var wg sync.WaitGroup
collectStar := func() { mu.Lock(); stars++; fmt.Println("Stars:", stars); mu.Unlock() }
loseStar := func() { mu.Lock(); stars--; fmt.Println("Stars:", stars); mu.Unlock() }
wg.Add(2)
go func() { defer wg.Done(); collectStar() }()
go func() { defer wg.Done(); loseStar() }()
wg.Wait()
}
Counts collected stars using concurrent operations.