Skip to content

Commit

Permalink
feat(go): introduce Go module with basic example using channels
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
appleboy committed Dec 7, 2024
1 parent 2a31844 commit 6ec5356
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions example56-context-timeout/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example

go 1.23.1
27 changes: 27 additions & 0 deletions example56-context-timeout/main.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 6ec5356

Please sign in to comment.