-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Go modules and timeout handling examples
- Add `go.mod` file for module initialization in solution01 - Implement a basic Go program in solution01 to demonstrate handling timeouts with `time.After` - Add `go.mod` file for module initialization in solution02 - Implement a basic Go program in solution02 to demonstrate handling timeouts with `context.WithTimeout` Signed-off-by: appleboy <[email protected]>
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module example | ||
|
||
go 1.23.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func main() { | ||
output := make(chan int, 30) | ||
|
||
go func() { | ||
for i := 0; i < 30; i++ { | ||
output <- i | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
}() | ||
|
||
timeout := time.After(1 * time.Second) | ||
// how to fix the timeout issue? | ||
for { | ||
select { | ||
case val := <-output: | ||
select { | ||
case <-timeout: | ||
println("reached timeout, but still have data to process") | ||
return | ||
default: | ||
} | ||
// simulate slow consumer | ||
time.Sleep(500 * time.Millisecond) | ||
println("output:", val) | ||
// how to fix the timeout issue? | ||
case <-timeout: | ||
println("timeout") | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module example | ||
|
||
go 1.23.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func main() { | ||
output := make(chan int, 30) | ||
|
||
go func() { | ||
for i := 0; i < 30; i++ { | ||
output <- i | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
}() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
// how to fix the timeout issue? | ||
for { | ||
select { | ||
case val := <-output: | ||
if ctx.Err() != nil { | ||
println("reached timeout, but still have data to process") | ||
return | ||
} | ||
// simulate slow consumer | ||
time.Sleep(500 * time.Millisecond) | ||
println("output:", val) | ||
// how to fix the timeout issue? | ||
case <-ctx.Done(): | ||
println("timeout") | ||
return | ||
} | ||
} | ||
} |