56 lines
890 B
Go
56 lines
890 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func sendA(ch chan string) {
|
||
|
fmt.Println("start waiting A")
|
||
|
time.Sleep(20 * time.Second)
|
||
|
fmt.Println("end waiting A")
|
||
|
ch <- "a"
|
||
|
close(ch)
|
||
|
}
|
||
|
|
||
|
func sendB(ch chan string) {
|
||
|
fmt.Println("start waiting B")
|
||
|
time.Sleep(10 * time.Second)
|
||
|
fmt.Println("end waiting B")
|
||
|
ch <- "b"
|
||
|
close(ch)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
if err := os.Chdir(""); err != nil {
|
||
|
fmt.Println("didn't change directory")
|
||
|
}
|
||
|
wg := sync.WaitGroup{}
|
||
|
wg.Add(2)
|
||
|
chA := make(chan string)
|
||
|
chB := make(chan string)
|
||
|
fmt.Println("start sendA")
|
||
|
go sendA(chA)
|
||
|
fmt.Println("start sendB")
|
||
|
go sendB(chB)
|
||
|
fmt.Println("start printing A")
|
||
|
go printCock(chA, &wg)
|
||
|
fmt.Println("start printing B")
|
||
|
go printCock(chB, &wg)
|
||
|
wg.Wait()
|
||
|
}
|
||
|
|
||
|
func printCock(ch chan string, wg *sync.WaitGroup) {
|
||
|
for {
|
||
|
select {
|
||
|
case x := <-ch:
|
||
|
log.Println(x)
|
||
|
wg.Done()
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|