You can create a
timeout with the
select statement. To use timeouts with concurrent goroutines, you must
Then, create a channel with
time.After() which as parameter takes time. The call
time.After(time.Second) would fill the channel after a second.
Related course: Introducing Go: Build Reliable, Scalable Programs
select, a simple timeout program can be created:
This will print “Timeout!” after the time has passed.
Timeouts can be combined with goroutine calls. Call a goroutine
f1 with a channel
go f1(c1). The goroutine writes in to the channel
c <- "message" after waiting 10 seconds.
Then a timeout is made with
time.After(). As this:
See what happens.