package main import ( "fmt" "github.com/robfig/cron/v3" "strconv" "testing" "time" ) func TestMonth(T *testing.T) { // 创建2023年12月31日的时间对象 date := time.Date(2023, time.August, 30, 0, 0, 0, 0, time.UTC) next := date.AddDate(0, -1, -1) month := int(next.Month()) monthStr := strconv.Itoa(month) year := next.Year() yearStr := strconv.Itoa(year) //预处理索引名称 PreBiddingIndex := "bidding_" + yearStr + monthStr fmt.Sprintf(PreBiddingIndex) } func TestCronExpression(t *testing.T) { // 模拟指定时间 //testTime := time.Date(2022, time.January, 31, 23, 59, 59, 0, time.UTC) //c := cron.New(cron.WithLocation(testTime.Local().Location())) local, _ := time.LoadLocation("Asia/Shanghai") c := cron.New(cron.WithLocation(local), cron.WithSeconds()) // 定义一个任务函数 task := func() { fmt.Println("222222") } // 创建一个cron表达式 spec := "* * * * * *" //spec := "59 59 23 * * *" // 将任务和cron表达式添加到调度器中,并使用测试时间 _, err := c.AddFunc(spec, task) if err != nil { t.Fatalf("添加定时任务失败:%v", err) } // 启动调度器 c.Start() defer c.Stop() select {} } func TestDeleteIndex(t *testing.T) { data := map[string]string{ "aa": "aaa", "bb": "bbb", "cc": "ccc", } for _, v := range data { if v == "aaa" { return } fmt.Println(v) } }