mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 21:24:46 +01:00
code kata 230829
This commit is contained in:
46
code_kata_230828/main.go
Normal file
46
code_kata_230828/main.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ch1 := make(chan struct{})
|
||||
ch2 := make(chan struct{})
|
||||
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
|
||||
go func() {
|
||||
go f1(ch1)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("f1 timeout")
|
||||
break
|
||||
case <-ch1:
|
||||
fmt.Println("f1 done")
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
go f2(ch2)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("f2 timeout")
|
||||
break
|
||||
case <-ch2:
|
||||
fmt.Println("f2 done")
|
||||
}
|
||||
}()
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
||||
func f1(in chan struct{}) {
|
||||
time.Sleep(1 * time.Second)
|
||||
in <- struct{}{}
|
||||
}
|
||||
|
||||
func f2(in chan struct{}) {
|
||||
time.Sleep(3 * time.Second)
|
||||
in <- struct{}{}
|
||||
}
|
||||
Reference in New Issue
Block a user