英文:
Using *gin.Context in cron Job
问题
我有一个cron job,它会调用一个需要*gin.Context作为参数的函数,这个参数将在其他步骤中的其他进程中被需要。以前我把我的代码写成这样:
_, _ = c.cr.AddFunc(constant.CronRunningAt(8), func() {
ctx := &gin.Context{}
c.loan.LoanRepaymentNotification(ctx)
})
但是它会抛出如下错误:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x38 pc=0x100d154f4]
有没有办法生成一个可以传递给被调用函数的*gin.Context的值类型?谢谢。
英文:
I have cron job that will call function that require *gin.Context as statement, this statement will be required int other process in the other next step. Previously I had made my code like this:
_, _ = c.cr.AddFunc(constant.CronRunningAt(8), func() {
ctx := &gin.Context{}
c.loan.LoanRepaymentNotification(ctx)
})
but it throw error like this:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x38 pc=0x100d154f4]
Is there any way to generate value type *gin.Context that can be passed to the function called? Thank you
答案1
得分: 0
如果您需要在cron任务中使用此方法,那么就意味着不能使用gin.Context
。
正如库文档中所指定的那样:
> Gin是一个用Go语言编写的Web框架。
因此,您可以通过另一种方式实现对gin.Context
的需求,例如使用context.Context
。
具体取决于您最终想要做什么,但您应该通过更改LoanRepaymentNotification
或其底层方法,仅在cron作业中使用context.Context
来重构您的代码。
英文:
If you need to use this method on a cron task, then it means that must not use gin.Context
.
As it is specified on library documentation:
> Gin is a web framework written in Go.
So what you want to do with gin.Context
could be achieve by another way, thanks to the context.Context
for exemple.
It depends of what you want to do at the end, but you should refactoring your code by changing LoanRepaymentNotification
or its underlying methods to use only context.Context
on your cron job.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论