Sobes.tech
Junior — Senior

Determining the maximum even number with concurrent access and race condition detection

livecode

Task condition

This exercise considers a program that attempts to find the largest even number in the range from 1 to 1000 using multiple goroutines. The steps are:

  • Predict what result will be displayed on the screen.
  • Identify errors in the implementation (data races, incorrect final result, etc.).
  • Suggest correct changes that fix the issues.
import "fmt"

func main() {
    var max int
            
    for i := 1000; i > 0; i-- {
        i := i
        go func () {
            if i%2 == 0 && i > max {
                max = i
            }
        }()
    }
    fmt.Printf("Maximum is %d",max)
}