mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-10 15:21:18 +00:00
41 lines
751 B
Go
41 lines
751 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func waitForDbs(numDBs int, dbChan chan struct{}) {
|
|
for i := 0; i < numDBs; i++ {
|
|
<-dbChan
|
|
}
|
|
}
|
|
|
|
// don't touch below this line
|
|
|
|
func test(numDBs int) {
|
|
dbChan := getDatabasesChannel(numDBs)
|
|
fmt.Printf("Waiting for %v databases...\n", numDBs)
|
|
waitForDbs(numDBs, dbChan)
|
|
time.Sleep(time.Millisecond * 10) // ensure the last print statement happens
|
|
fmt.Println("All databases are online!")
|
|
fmt.Println("=====================================")
|
|
}
|
|
|
|
func main() {
|
|
test(3)
|
|
test(4)
|
|
test(5)
|
|
}
|
|
|
|
func getDatabasesChannel(numDBs int) chan struct{} {
|
|
ch := make(chan struct{})
|
|
go func() {
|
|
for i := 0; i < numDBs; i++ {
|
|
ch <- struct{}{}
|
|
fmt.Printf("Database %v is online\n", i+1)
|
|
}
|
|
}()
|
|
return ch
|
|
}
|