V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
zxCoder
V2EX  ›  问与答

golang 这个 panic 老是整不明白

  •  
  •   zxCoder · 2020-10-22 11:38:11 +08:00 · 1796 次点击
    这是一个创建于 1497 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看了教程都是些简单的例子,结果写个程序一出错连哪一行出错都没说,也不知道咋办。。。

    runtime error: invalid memory address or nil pointer dereference
    /home/keane/app/go/src/runtime/panic.go:199 (0x443fcb)
            panicmem: panic(memoryError)
    /home/keane/app/go/src/runtime/signal_unix.go:394 (0x443e08)
            sigpanic: panicmem()
    /home/keane/go/pkg/mod/github.com/jinzhu/[email protected]/main.go:853 (0x969875)
            (*DB).clone: dialect:           newDialect(s.dialect.GetName(), s.db),
    /home/keane/go/pkg/mod/github.com/jinzhu/[email protected]/main.go:204 (0x96291e)
            (*DB).NewScope: dbClone := s.clone()
    /home/keane/go/pkg/mod/github.com/jinzhu/[email protected]/main.go:354 (0x96503b)
            (*DB).Find: return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
    /home/keane/GolandProjects/poblog/services/postService.go:168 (0x99722c)
            (*PostService).List: db.Find(&posts)
    /home/keane/GolandProjects/poblog/controllers/postController.go:11 (0x9973ac)
            ListPost: _,_=postService.List()
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x8f2d1a)
            (*Context).Next: c.handlers[c.index](c)
    /home/keane/GolandProjects/poblog/middlewares/corsMiddleware.go:23 (0x99747c)
            Cors.func1: c.Next()
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x8f2d1a)
            (*Context).Next: c.handlers[c.index](c)
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:83 (0x906b43)
            RecoveryWithWriter.func1: c.Next()
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x8f2d1a)
            (*Context).Next: c.handlers[c.index](c)
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:241 (0x905c70)
            LoggerWithConfig.func1: c.Next()
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x8f2d1a)
            (*Context).Next: c.handlers[c.index](c)
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:409 (0x8fcf8c)
            (*Engine).handleHTTPRequest: c.Next()
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:367 (0x8fc68d)
            (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
    /home/keane/app/go/src/net/http/server.go:2802 (0x6cb8a3)
            serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
    /home/keane/app/go/src/net/http/server.go:1890 (0x6c7144)
            (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
    /home/keane/app/go/src/runtime/asm_amd64.s:1357 (0x45c0c0)
            goexit: BYTE    $0x90   // NOP
    
    
    
    
    linxl
        1
    linxl  
       2020-10-22 11:47:07 +08:00
    ```
    /home/keane/GolandProjects/poblog/services/postService.go:168 (0x99722c)
    (*PostService).List: db.Find(&posts)
    /home/keane/GolandProjects/poblog/controllers/postController.go:11 (0x9973ac)
    ListPost: _,_=postService.List()
    /home/keane/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x8f2d1a)
    (*Context).Next: c.handlers[c.index](c)
    /home/keane/GolandProjects/poblog/middlewares/corsMiddleware.go:23 (0x99747c)
    Cors.func1: c.Next()
    ```
    这里面其中一行?
    BoarBoar
        2
    BoarBoar  
       2020-10-22 11:50:38 +08:00
    你这是 gorm 抛的错误吧,调用栈里没有业务代码当然不会报行数
    这种情况问题多半在 init 函数里,又是空指针,多半是 gorm 那个 DB 什么的全局变量没初始化
    最后提醒一句,慎用 init,必须用的时候越简单越好
    labulaka521
        3
    labulaka521  
       2020-10-22 11:53:18 +08:00 via iPhone
    那你倒是把代码贴出来 这样咋看
    xkeyideal
        4
    xkeyideal  
       2020-10-22 11:53:42 +08:00
    s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
    /home/keane/GolandProjects/poblog/services/postService.go:168 (0x99722c)
    (*PostService).List: db.Find(&posts
    已经很清楚的告诉你了,db 这个变量是空指针

    水平有待提高啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5488 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 08:12 · PVG 16:12 · LAX 00:12 · JFK 03:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.