英文:
Panic error when trying to serve a Gin Gonic app
问题
我正在尝试使用Gin框架创建一个小型API,但在尝试将其拆分为多个文件时遇到了错误。由于我在Go语言方面是一个绝对的初学者,所以可能犯了一些很大的错误,请多多包涵
我的项目结构如下:
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()
// API的v1版本
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()
// 监听并在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
这很奇怪,因为我没有看到任何与我的代码相关的引用。我找到了一些类似的问题,像这个,但是所有这些问题似乎在堆栈跟踪的顶部都有自己代码的错误。据我所见,我只得到了框架错误。
对于这里可能出错的原因有什么想法吗?
提前感谢。
英文:
I'm trying to create a small API using Gin framework for Go, and I'm getting an error when trying to split it in many files. Since I'm an absolute beginner in Go, I'm probably doing some big stupid mistake, so please bear with me
My project structure is this:
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)
}
When I run the app, it seems to start Ok
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
But when trying to navigate to http://localhost:8080/v1/notes
, I get this error:
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
Which is strange, because I don't see any reference to my own code. I have found some similar questions, like this, but all of them seem to have errors on their own code at the top of the stack trace. For I can see, I'm only getting framework errors.
Any ideas on what could be going wrong here?
Thanks in advance
答案1
得分: 1
更改映射的初始化
var Router *gin.Engine
func CreateUrlMappings() {
Router = gin.Default()
// API的v1版本
v1 := Router.Group("/v1")
{
v1.GET("/notes", controllers.AllNotesEndpoint)
}
}
当你使用":="运算符时,你正在创建一个新的局部变量
Router := gin.Default() // 如果你正在使用全局变量,这是错误的
英文:
Change the initialization on mappings
var Router *gin.Engine
func CreateUrlMappings() {
Router = gin.Default()
// v1 of the API
v1 := Router.Group("/v1")
{
v1.GET("/notes", controllers.AllNotesEndpoint)
}
}
You're creating a new local variable when you use ":=" operator
Router := gin.Default() // this is wrong if you are using a global var
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论