我正在尝试使用Go的Gin框架创建一个小API,当我试图将它拆分到许多文件中时,会遇到一个错误。因为我是围棋的新手,所以我可能犯了一个很愚蠢的错误,所以请原谅我:)
我的项目结构是:

models.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)
    }
}main.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这很奇怪,因为我没有看到任何对我自己的代码的引用。我发现了一些类似的问题,像这样,但它们似乎都在堆栈跟踪的顶部对自己的代码有错误。因为我可以看到,我只得到框架错误。
对这里可能出什么问题有什么想法吗?
提前感谢
发布于 2016-11-26 01:30:43
更改映射的初始化
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 varhttps://stackoverflow.com/questions/40813776
复制相似问题