Search⌘ K
AI Features

Solution: Loops

Explore how to manage loops in Go, including counting prime numbers and handling goroutines properly. Understand why starting loops at specific points matters and how to pass variables to goroutines to avoid common bugs. This lesson helps you write more reliable and efficient Go code using control structures.

We'll cover the following...

Solution

...
Go (1.16.5)
package main
import (
"fmt"
"math"
"sync"
"time"
)
var (
mu sync.Mutex
)
func isPrime(num int) bool {
for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
if int(num)%i == 0 {
return false
}
}
return true
}
func findPrimes(resultMap map[int]struct{}, startFrom, tryCount int) {
for i := startFrom; i < startFrom+tryCount; i++ {
if isPrime(i) {
mu.Lock()
resultMap[i] = struct{}{}
mu.Unlock()
}
}
}
func main() {
resultMap := make(map[int]struct{})
for i := 0; i < 100; i++ {
go func(i int) {
findPrimes(resultMap, 99999*i+2, 1000)
}(i)
}
time.Sleep(time.Second * 3)
fmt.Printf("Found %v primes\n", len(resultMap))
if len(resultMap) < 6746 {
panic("did not find all the prime numbers")
}
}

Explanation

...