Learn Go - 9 Code Examples & CST Typing Practice Test
Go (Golang) is a statically typed, compiled programming language designed at Google. It emphasizes simplicity, concurrency, and high-performance networking and system programming, making it ideal for cloud services, web backends, and distributed systems.
Learn GO with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Go
What is Go?
Go (Golang) is a statically typed, compiled programming language designed at Google. It emphasizes simplicity, concurrency, and high-performance networking and system programming, making it ideal for cloud services, web backends, and distributed systems.
What are the primary use cases for Go?
Backend web services and APIs. Cloud-native and distributed systems. Command-line utilities. Network programming and microservices. DevOps and infrastructure tooling
What are the strengths of Go?
High performance due to compilation. Concurrency primitives built-in and easy to use. Strong standard library for common tasks. Cross-platform compilation. Easy deployment as a single statically linked binary
What are the limitations of Go?
No generics before Go 1.18 (now available but limited). Minimalist standard library for GUI or graphics. Error handling requires explicit checks. Limited metaprogramming or macros. Lacks some modern language features like operator overloading
How can I practice Go typing speed?
CodeSpeedTest offers 9+ real Go code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.