Skip to content

Commit 6ec5356

Browse files
committed
feat(go): introduce Go module with basic example using channels
- Add `go.mod` file for Go module initialization - Create `main.go` with a basic example of using channels and timeouts in Go Signed-off-by: appleboy <[email protected]>
1 parent 2a31844 commit 6ec5356

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

example56-context-timeout/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example
2+
3+
go 1.23.1

example56-context-timeout/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"time"
5+
)
6+
7+
func main() {
8+
output := make(chan int, 1)
9+
10+
go func() {
11+
for i := 0; i < 30; i++ {
12+
output <- i
13+
time.Sleep(100 * time.Millisecond)
14+
}
15+
}()
16+
17+
for {
18+
select {
19+
case val := <-output:
20+
println("output:", val)
21+
// how to fix the timeout issue?
22+
case <-time.After(1 * time.Second):
23+
println("timeout")
24+
return
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)