# gotask **Repository Path**: pysrc/gotask ## Basic Information - **Project Name**: gotask - **Description**: 简单的go定时任务 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-03-22 - **Last Updated**: 2021-03-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 定时任务 最小堆实现的定时任务 ## 安装&使用 `go get github.com/pysrc/gotask` ```go package main import ( "log" "time" "github.com/pysrc/gotask" ) func main() { var cur = time.Now().Unix() log.Println("Start", cur) // 12s后执行 gotask.Push(&gotask.Task{ Start: cur + 12, Run: func() { log.Println(time.Now().Unix(), "12 step 0") }, }) // 3s后执行, 且每2s执行一次,9s后不再执行 var max = time.Now().Add(time.Second * 9).Unix() gotask.Push(&gotask.Task{ Start: cur + 3, Run: func() { log.Println(time.Now().Unix(), "3 step 2") }, Next: func() int64 { var n = time.Now().Add(time.Second * 2).Unix() if n < max { return n } return 0 }, }) // 5s后执行,且中途panic测试 gotask.Push(&gotask.Task{ Start: cur + 5, Run: func() { log.Println(time.Now().Unix(), "5 step 0") panic("Task run error test") }, }) time.Sleep(time.Second * 14) } ```