dtll2016 2018-03-13 04:20
浏览 61
已采纳

紧急情况:运行时错误:无效的内存地址或在拆分应用程序时取消引用nil指针

I am trying to compile my go app but am getting the following error:

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x14d6572]
goroutine 1 [running]: 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

This is my main:

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()
}

This is my 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)

    }

}

I am still pretty new to Go so I am not exactly sure what is going on. However this program originally started with all the files in the same package but I am now trying to separate it. It worked before the split so might be just the way I am calling it?

  • 写回答

1条回答 默认 最新

  • dseve40868 2018-03-13 04:30
    关注

    You have a var router *gin.Engine inside the InitializeRoutes() function, you don't set router after that line to anything so its nil when you try to use later in the function. This one declared inside the function is shadowing the package level one and causing your problems, delete that line.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)