How would you implement a timeout using channels?
- Use a select statement with a default case.
- Use a mutex to lock the channel for a specified duration.
- Use a timer object provided by the standard library.
- Use a for loop with a sleep statement.
To implement a timeout using channels in Go, you can use a select statement with a default case. This allows you to wait for data from a channel for a specified period, and if no data arrives within that time, you can execute a timeout action. It's a clean and efficient way to handle timeouts in concurrent code.
Loading...
Related Quiz
- How can you use the go test command to run a specific test function?
- Goroutines communicate via _____ to ensure synchronized access to shared data.
- Explain how you would use benchmarking in conjunction with profiling to optimize a Go application.
- Explain a situation where dependency injection could simplify the process of mocking external services in a Go application.
- Maps in Go are not _____ by default, which means the order of keys when iterating over a map can change.