文件

快速開始

安裝 go-scheduler 並執行第一個排程任務。

前置需求

安裝

使用 go get

go get github.com/pardnchiu/go-scheduler

從原始碼建置

git clone https://github.com/pardnchiu/go-scheduler.git
cd go-scheduler
go build ./...

快速上手

建立排程器、新增任務並啟動:

package main

import (
    "fmt"
    "time"

    "github.com/pardnchiu/go-scheduler/core"
)

func main() {
    c, err := core.New(core.Config{})
    if err != nil {
        panic(err)
    }

    id, err := c.Add("@every 30s", func() {
        fmt.Println("hello", time.Now())
    }, "heartbeat")
    if err != nil {
        panic(err)
    }
    fmt.Println("task id:", id)

    c.Start()
    defer func() {
        ctx := c.Stop()
        <-ctx.Done()
    }()

    time.Sleep(2 * time.Minute)
}

指定時區

透過 Config.Location 傳入 *time.Location。為 nil 時使用 time.Local

loc, err := time.LoadLocation("Asia/Taipei")
if err != nil {
    panic(err)
}

c, err := core.New(core.Config{
    Location: loc,
})
if err != nil {
    panic(err)
}

下一步

EN