Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Branching and Looping in GO
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
go findRandomNumber(rand.Intn(100))
time.Sleep(5 * time.Second)
}
func findRandomNumber(randomNumber int) {
count := 1
numberFound := false
for {
number := rand.Intn(10000)
if number == randomNumber {
numberFound = true
break
}
count++
}
if numberFound {
fmt.Printf("Number #%v found after %v attempt(s)", randomNumber, count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.