Standard library / time

time.AfterFunc()

Schedule f in its own goroutine after d. Its timer has no channel delivery; Stop does not wait for a running callback. Expected stdout: 'callback finished\n'.

Formal Syntax & Structure

go syntaxgo
func AfterFunc(d Duration, f func()) *Timer

Parameters & Attributes

NameTypeDescription
dDurationElapsed duration or interval.
ffunc()Callback executed in its own goroutine.

Return Value

*Timer

Schedule f in its own goroutine after d.

Working Example

Example Snippetgo
package main
import (  "fmt"  "time")
func main() {  done := make(chan struct{})  timer := time.AfterFunc(0, func() { close(done) })  defer timer.Stop()  <-done  fmt.Println("callback finished")}
Go Reference