gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:14:28 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com