func (timeTables) GetTimeTables(c echo.Context) error {
resp, err := http.Get("url")
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
test := &models.MultiModel{}
json.Unmarshal(body, &test)
return c.JSON(http.StatusOK,"")
}
1小时后如何调用func GetTimeTables,我使用AferFunc,我想念什么?
func (timeTables) CallFunc() {
time.AfterFunc(4*time.Hour, func() {
GetTimeTables(c echo.Context)
})
}
最佳答案
您的程序需要阻止,否则它将在触发AfterFunc
之前退出。
尝试从此示例https://play.golang.org/p/_25RffWDyPV中删除 sleep 以进行查看。GetTimeTables
不是可以直接调用的函数,您需要将其作为timeTables对象的方法来调用。另外,如@icza所述,您应该将一个值传递给该函数。
这样的事情应该起作用:
func (t timeTables) CallFunc(c echo.Context) {
time.AfterFunc(4*time.Hour, func() {
t.GetTimeTables(c)
})
}
关于go - N小时后如何调用函数X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59385310/