time.After() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The After() function in Go language is used to wait for the duration of time to pass and after that it delivers the actual time on the returned channel. Moreover, this function is defined under the time package. Here, you need to import “time” package in order to use these functions.
func After(d Duration) <-chan Time
Here, d is the duration of time before time out, and chan is the channel where current time is sent.
Return Value: It first waits for the stated time and then displays timeout.
****Welcome to GeeksforGeeks*** A CS-Portal! ****Welcome to GeeksforGeeks*** A CS-Portal! ****Welcome to GeeksforGeeks*** A CS-Portal! ****Welcome to GeeksforGeeks*** A CS-Portal! ****Welcome to GeeksforGeeks*** A CS-Portal! Time Out! // Displayed after 3 seconds as mentioned in the above code
In the above example, we have used the “case” statement under the select statement in order to send operation on the channel. Moreover, here time out will be displayed after 3 seconds of the execution of for loop.
Here, we have used the “make” keyword in order to create a channel then like the above example here also case statement is used under a select statement but here it’s used twice. The first one is used to return output and the second one is used to call After() method on the channel. After this, the timeout is displayed in the stated time.