In Go language, time packages supplies functionality for determining as well as viewing time. The AfterFunc() function in Go language is used to wait for the duration of time to pass and after that, it calls the defined function “f” in its own go-routine. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.
func AfterFunc(d Duration, f func()) *Timer
Here, *Timer is a pointer to the Timer.
Return Value: It returns a Timer which is then used to cancel the call with the help of its Stop() method.
Function called by AfterFunc() after 3 seconds
Here, the output is returned after 3 seconds and then the returned timer cancels the call to the function using Stop() method. After then the program is exited after the duration of sleep ends.
time to wait time to wait 6 seconds over.... 30 is arriving Done!
In the above example, after the stated duration is over then channel returns its output and the program exits.
- How to convert a slice of bytes in uppercase in Golang?
- Golang program that uses fallthrough keyword
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- How to check equality of slices of bytes in Golang?
- atomic.AddInt64() Function in Golang With Examples
- atomic.StoreInt64() Function in Golang With Examples
- reflect.FieldByIndex() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- bits.Sub() Function in Golang with Examples
- How to convert a slice of bytes in lowercase in Golang?
- io.PipeWriter.CloseWithError() Function in Golang with Examples
- Import in GoLang
- time.Round() Function in Golang With Examples
- How to add a method to struct type in Golang?
- Converting a string variable into Boolean, Integer or Float type in Golang
- Check if the given slice is sorted in Golang
- How to compare times in Golang?
- reflect.AppendSlice() Function in Golang with Examples
- reflect.ChanOf() Function in Golang with Examples