go - 尝试提供 Gin Gonic 应用程序时出现紧急错误

标签 go

我正在尝试使用 Gin framework for Go 创建一个小型 API ,并且在尝试将其拆分为多个文件时出现错误。由于我是 Go 的绝对初学者,我可能犯了一些愚蠢的大错误,所以请多多包涵 :)
我的项目结构是这样的:

project structure

模型.go

package models

type Note struct {
    Title   string `form:"title" json:"title" binding:"required"`
    Body    string `form:"body" json:"body" binding:"required"`
}

var Notes []Note

func MockData() {
    Notes = append(Notes, Note{Title: "My first note", Body: "Brazil beated Argentina very badly :("})
    Notes = append(Notes, Note{Title: "Some more notes", Body: "I hope we can defeat Colombia"})
}

url_mappings.go

package mappings

import (
    "gopkg.in/gin-gonic/gin.v1"
    "github.com/juanmougan/notepad/api/controllers"
)

var Router *gin.Engine

func CreateUrlMappings() {
    Router := gin.Default()
    // v1 of the API
    v1 := Router.Group("/v1")
    {
        v1.GET("/notes", controllers.AllNotesEndpoint)
    }
}

主.go

package main

import (
    "github.com/juanmougan/notepad/api/models"
    "github.com/juanmougan/notepad/api/mappings"
)

func main() {
    // TODO eventually use a real DB
    models.MockData()
    mappings.CreateUrlMappings()
    // Listen and server on 0.0.0.0:8080
    mappings.Router.Run(":8080")
}

notes_controllers.go

package controllers

import (
    "gopkg.in/gin-gonic/gin.v1"
    "net/http"
    "github.com/juanmougan/notepad/api/models"
)

func AllNotesEndpoint(c *gin.Context) {
    c.JSON(http.StatusOK, models.Notes)
}

当我运行应用程序时,它似乎开始正常

MacBook-Pro-de-Juan:notepad juanma$ go run api/main/main.go 
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /v1/notes                 --> github.com/juanmougan/notepad/api/controllers.AllNotesEndpoint (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080

但是当尝试导航到 http://localhost:8080/v1/notes 时,我收到此错误:

http: panic serving [::1]:50178: runtime error: invalid memory address or nil pointer dereference
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc420166400)
    /usr/local/opt/go/libexec/src/net/http/server.go:1491 +0x12a
panic(0x396220, 0xc42000c0b0)
    /usr/local/opt/go/libexec/src/runtime/panic.go:458 +0x243
gopkg.in/gin-gonic/gin%2ev1.(*Engine).ServeHTTP(0x0, 0x5a7c80, 0xc4201ec0d0, 0xc4200f0870)
    /Users/juanma/.go/src/gopkg.in/gin-gonic/gin.v1/gin.go:260 +0x26
net/http.serverHandler.ServeHTTP(0xc420166380, 0x5a7c80, 0xc4201ec0d0, 0xc4200f0870)
    /usr/local/opt/go/libexec/src/net/http/server.go:2202 +0x7d
net/http.(*conn).serve(0xc420166400, 0x5a83c0, 0xc420174440)
    /usr/local/opt/go/libexec/src/net/http/server.go:1579 +0x4b7
created by net/http.(*Server).Serve
    /usr/local/opt/go/libexec/src/net/http/server.go:2293 +0x44d
http: panic serving [::1]:50179: runtime error: invalid memory address or nil pointer dereference
goroutine 36 [running]:
net/http.(*conn).serve.func1(0xc420166600)
    /usr/local/opt/go/libexec/src/net/http/server.go:1491 +0x12a
panic(0x396220, 0xc42000c0b0)
    /usr/local/opt/go/libexec/src/runtime/panic.go:458 +0x243
gopkg.in/gin-gonic/gin%2ev1.(*Engine).ServeHTTP(0x0, 0x5a7c80, 0xc420214000, 0xc4200f0960)
    /Users/juanma/.go/src/gopkg.in/gin-gonic/gin.v1/gin.go:260 +0x26
net/http.serverHandler.ServeHTTP(0xc420166380, 0x5a7c80, 0xc420214000, 0xc4200f0960)
    /usr/local/opt/go/libexec/src/net/http/server.go:2202 +0x7d
net/http.(*conn).serve(0xc420166600, 0x5a83c0, 0xc420174600)
    /usr/local/opt/go/libexec/src/net/http/server.go:1579 +0x4b7
created by net/http.(*Server).Serve
    /usr/local/opt/go/libexec/src/net/http/server.go:2293 +0x44d

这很奇怪,因为我没有看到任何对我自己的代码的引用。我发现了一些类似的问题,like this ,但他们似乎都在堆栈跟踪顶部的自己的代码上有错误。据我所知,我只收到框架错误。
关于这里可能出什么问题的任何想法?

提前致谢

最佳答案

更改映射的初始化

var Router *gin.Engine

func CreateUrlMappings() {
    Router = gin.Default()
    // v1 of the API
    v1 := Router.Group("/v1")
    {
        v1.GET("/notes", controllers.AllNotesEndpoint)
    }
}

当您使用“:=”运算符时,您正在创建一个新的局部变量

Router := gin.Default()  // this is wrong if you are using a global var

关于go - 尝试提供 Gin Gonic 应用程序时出现紧急错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40813776/

相关文章:

go - 通过 switch 语句分配类型/创建结构

bash - 从 Golang 执行 Bash 脚本

postgresql - go Scratch 容器和 PG 容器之间的 SSL 连接问题。如何解决?

go - 如何将多个并发请求分配给 aws lambda 函数?

go - 如何创建自定义工作流定义?

angularjs - 在 AngularJS http 数据字段中传递的数据

go - 如何详细打印结构并隐藏一些字段?

go - 为什么 "infinite"for 循环没有被处理?

html - 如何从 Golang 中的 html/template 中删除 ZgotmplZ?

regex - 如何在golang中使用正则表达式替换字符串中的表情符号字符