【问题标题】:panic: runtime error: invalid memory address or nil pointer dereference on breaking up application恐慌:运行时错误:无效的内存地址或零指针取消引用分解应用程序
【发布时间】:2018-08-21 05:50:56
【问题描述】:

我正在尝试编译我的 go 应用,但出现以下错误:

panic:运行时错误:无效的内存地址或 nil 指针取消引用 [信号SIGSEGV:分段违规代码=0x1 addr=0x0 pc=0x14d6572]
goroutine 1 [运行]: github.com/gin-gonic/gin.(*Engine).Use(0x0, 0xc420201f30, 0x1, 0x1, 0x2, 0x2) /Users/jordan.kasper/go/src/github.com/gin-gonic/gin/gin.go:227 +0x22 gin-sample-app/handlers.InitializeRoutes() /Users/jordan.kasper/go/src/gin-sample-app/handlers/routes.go:15 +0x61 main.main() /Users/jordan.kasper/go/src/gin-sample-app/main.go:25 +0x77

这是我的主要内容:

package main

import (
    "gin-sample-app/handlers"

    "github.com/gin-gonic/gin"
)

var router *gin.Engine

func main() {
    // Set Gin to production mode
    gin.SetMode(gin.ReleaseMode)

    // Set the router as the default one provided by Gin
    router = gin.Default()

    // Process the templates at the start so that they don't have to be loaded
    // from the disk again. This makes serving HTML pages very fast.
    router.LoadHTMLGlob("templates/*")

    // Initialize the routes
    handlers.InitializeRoutes()

    // Start serving the application
    router.Run()
}

这是我的 InitializeRoutes:

package handlers

import (
    "gin-sample-app/middleware"

    "github.com/gin-gonic/gin"
)

// initializing the routes
func InitializeRoutes() {

    var router *gin.Engine

    // Use the setUserStatus middleware for every route to set a flag
    // indicating whether the request was from an authenticated user or not
    router.Use(middleware.SetUserStatus())

    // Handle the index route
    router.GET("/", ShowIndexPage)

    // Group user related routes together
    userRoutes := router.Group("/u")
    {
        // Handle the GET requests at /u/login
        // Show the login page
        // Ensure that the user is not logged in by using the middleware
        userRoutes.GET("/login", middleware.EnsureNotLoggedIn(), showLoginPage)

        // Handle POST requests at /u/login
        // Ensure that the user is not logged in by using the middleware
        userRoutes.POST("/login", middleware.EnsureNotLoggedIn(), performLogin)

        // Handle GET requests at /u/logout
        // Ensure that the user is logged in by using the middleware
        userRoutes.GET("/logout", middleware.EnsureLoggedIn(), logout)

        // Handle the GET requests at /u/register
        // Show the registration page
        // Ensure that the user is not logged in by using the middleware
        userRoutes.GET("/register", middleware.EnsureNotLoggedIn(), showRegistrationPage)

        // Handle POST requests at /u/register
        // Ensure that the user is not logged in by using the middleware
        userRoutes.POST("/register", middleware.EnsureNotLoggedIn(), register)
    }

    // Group article related routes together
    articleRoutes := router.Group("/article")
    {
        // Handle GET requests at /article/view/some_article_id
        articleRoutes.GET("/view/:article_id", getArticle)

        // Handle the GET requests at /article/create
        // Show the article creation page
        // Ensure that the user is logged in by using the middleware
        articleRoutes.GET("/create", middleware.EnsureLoggedIn(), showArticleCreationPage)

        // Handle POST requests at /article/create
        // Ensure that the user is logged in by using the middleware
        articleRoutes.POST("/create", middleware.EnsureLoggedIn(), createArticle)

    }

}

我对 Go 还是很陌生,所以我不确定发生了什么。然而,这个程序最初是从同一个包中的所有文件开始的,但我现在正试图将它分开。它在拆分之前有效,所以可能就是我所说的方式?

【问题讨论】:

    标签: go


    【解决方案1】:

    您在 InitializeRoutes() 函数中有一个var router *gin.Engine,您没有将该行之后的路由器设置为任何内容,因此当您稍后尝试在该函数中使用时它为零。在函数内部声明的这一行隐藏了一级包并导致您的问题,请删除该行。

    【讨论】:

    • 如果我正确理解 InitalizeRoutes() 中的那个是不需要的,因为 main.go 中的包级别的那个?
    • 没关系,我明白你在说什么。谢谢!
    猜你喜欢
    • 2013-04-23
    • 2023-01-11
    • 1970-01-01
    • 2015-02-24
    • 2022-01-05
    • 2019-03-09
    • 1970-01-01
    • 2021-08-21
    • 2019-09-12
    相关资源
    最近更新 更多