【问题标题】:exit status 2 when running goroutine运行 goroutine 时退出状态 2
【发布时间】:2022-01-23 12:54:26
【问题描述】:

为什么我的程序在执行退出状态为 2 的 goroutine 时会出现恐慌/自动关闭

var (
    timeout time.Duration
    ctx     context.Context
)
synchronised := usecases.NewXbaseUsecase(timeout, xbaseRepo, oodoBaseUrl, oodoDb, oodoEmail, oodoPassword, httpCl)
count := 1
gocron.Every(1).Days().At("15:54").Do(func() {
    log.Println("Start Sync")
    log.Println("Fetch ->", count)
    synchronised.SyncServices(ctx)
})
<-gocron.Start()

错误消息:恐慌:无法从 nil 父级创建上下文

goroutine 10 [running]:
context.WithDeadline({0x0, 0x0}, {0xe14120, 0xee6b2800, 0xe14120})
        /home/sdk/go1.17.2/src/context/context.go:436 +0x41b

【问题讨论】:

  • 无论synchronised.SyncServices 是什么,它都可能期待一个非零上下文。 ctx = context.Background()?

标签: go


【解决方案1】:

看起来你没有给ctx我所看到的实际值。通常你会使用 ctx = context.Background()ctx = context.WithCancel(context.Background) 之类的东西

这里有一篇好文章:https://www.sohamkamani.com/golang/context-cancellation-and-values/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    相关资源
    最近更新 更多