Introduction
The purpose of this challenge is to implement timeouts in Go using channels and select
.
This tutorial is from open-source community. Access the source code
The purpose of this challenge is to implement timeouts in Go using channels and select
.
When programs connect to external resources or need to bound execution time, timeouts are important. The challenge is to implement timeouts in Go using channels and select
.
select
.time.After
to await a value to be sent after the timeout.select
to proceed with the first receive that's ready.## Running this program shows the first operation timing
## out and the second succeeding.
$ go run timeouts.go
timeout 1
result 2
In this challenge, we learned how to implement timeouts in Go using channels and select
. We used a buffered channel to prevent goroutine leaks in case the channel is never read, and time.After
to await a value to be sent after the timeout. We also used select
to proceed with the first receive that's ready.