diff --git a/example56-context-timeout/go.mod b/example56-context-timeout/go.mod new file mode 100644 index 0000000..3f0e511 --- /dev/null +++ b/example56-context-timeout/go.mod @@ -0,0 +1,3 @@ +module example + +go 1.23.1 diff --git a/example56-context-timeout/main.go b/example56-context-timeout/main.go new file mode 100644 index 0000000..b06d5ef --- /dev/null +++ b/example56-context-timeout/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "time" +) + +func main() { + output := make(chan int, 1) + + go func() { + for i := 0; i < 30; i++ { + output <- i + time.Sleep(100 * time.Millisecond) + } + }() + + for { + select { + case val := <-output: + println("output:", val) + // how to fix the timeout issue? + case <-time.After(1 * time.Second): + println("timeout") + return + } + } +}