Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Testing Concurrency – Race | Go Programming Bootcamp lesson | BraveProgrammer
  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 39 of 60

·Go

Testing Concurrency – Race Detector and `testing`

20 minutes

Course progress65%

Testing Concurrent Code

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

Race Detector

Run tests with -race to catch data races.

$ go test -race ./...

Example: Concurrent Counter

type Counter struct { n int; mu sync.Mutex }
 Inc() { c.mu.Lock(); c.n++; c.mu.Unlock() }
 Value()  { c.mu.Lock();  c.mu.Unlock();  c.n }
func (c *Counter)
func (c *Counter)
int
defer
return
func TestCounter(t *testing.T) {
    var c Counter
    var wg sync.WaitGroup
    for i := 0; i < 1000; i++ {
        wg.Add(1)
        go func() { c.Inc(); wg.Done() }()
    }
    wg.Wait()
    if got := c.Value(); got != 1000 { t.Fatalf("got %d, want 1000", got) }
}

The race detector will flag any missing synchronization.

Info

Even with the race detector, logic bugs (e.g., deadlocks) need careful design.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from Go Programming BootcampBrowse activities
Previous lessonWebSockets with `gorilla/websocket`Continue to lesson 40Building a Simple CLI Tool – Argument ParsingBack to Go Programming Bootcamp curriculum