I'm doing tests with gin and gorm , I have the following program:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
var db *gorm.DB
var err error
type Person struct {
ID uint 'gorm:"primary_key"'
FirstName string
LastName string
}
func main() {
// NOTE: See we’re using = to assign the global var
// instead of := which would assign it only in this function
db, err := gorm.Open("mysql", "santo:asdf@/test?charset=utf8&parseTime=True&loc=Local")
if err != nil {
fmt.Println(err)
}
defer db.Close()
db.AutoMigrate(&Person{})
r := gin.Default()
r.GET("/people/", GetPeople)
r.GET("/people/:id", GetPerson)
r.POST("/people", CreatePerson)
r.PUT("/people/:id", UpdatePerson)
r.DELETE("/people/:id", DeletePerson)
r.Run(":8080")
}
func DeletePerson(c *gin.Context) {
id := c.Params.ByName("id")
var person Person
d := db.Where("id = ?", id).Delete(&person)
fmt.Println(d)
c.JSON(200, gin.H{"id #" + id: "deleted"})
}
func UpdatePerson(c *gin.Context) {
var person Person
id := c.Params.ByName("id")
if err := db.Where("id = ?", id).First(&person).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
}
c.BindJSON(&person)
db.Save(&person)
c.JSON(200, person)
}
func CreatePerson(c *gin.Context) {
var person Person
c.BindJSON(&person)
db.Create(&person)
c.JSON(200, person)
}
func GetPerson(c *gin.Context) {
id := c.Params.ByName("id")
var person Person
if err := db.Where("id = ?", id).First(&person).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, person)
}
}
func GetPeople(c *gin.Context) {
var people []Person
if err := db.Find(&people).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, people)
}
}
The program compiles well, the problem is at the moment of execution, when I make the call to the rest service:
http://localhost:8080/people/
I should show an empty array: []
But instead it shows me the following error in the console:
runtime error: invalid memory address or nil pointer dereference
/usr/local/go/src/runtime/panic.go:502 (0x42ab48)
/usr/local/go/src/runtime/panic.go:63 (0x429bbd)
/usr/local/go/src/runtime/signal_unix.go:388 (0x43f8f9)
/home/santo/go/src/github.com/jinzhu/gorm/main.go:743 (0x8dab67)
/home/santo/go/src/github.com/jinzhu/gorm/main.go:168 (0x8d642e)
/home/santo/go/src/github.com/jinzhu/gorm/main.go:311 (0x8d7432)
(*DB).Find: return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
/home/santo/go/src/github.com/hectorgool/orm/main.go:79 (0x91a5bf)
GetPeople: if err := db.Find(&people).Error; err != nil {
/home/santo/go/src/github.com/gin-gonic/gin/context.go:107 (0x88e252)
(*Context).Next: c.handlers[c.index](c)
/home/santo/go/src/github.com/gin-gonic/gin/recovery.go:47 (0x89e639)
RecoveryWithWriter.func1: c.Next()
/home/santo/go/src/github.com/gin-gonic/gin/context.go:107 (0x88e252)
(*Context).Next: c.handlers[c.index](c)
/home/santo/go/src/github.com/gin-gonic/gin/logger.go:83 (0x89d8eb)
LoggerWithWriter.func1: c.Next()
/home/santo/go/src/github.com/gin-gonic/gin/context.go:107 (0x88e252)
(*Context).Next: c.handlers[c.index](c)
/home/santo/go/src/github.com/gin-gonic/gin/gin.go:359 (0x8959d5)
(*Engine).handleHTTPRequest: c.Next()
/home/santo/go/src/github.com/gin-gonic/gin/gin.go:326 (0x895222)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/go/src/net/http/server.go:2694 (0x66520b)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/go/src/net/http/server.go:1830 (0x6613c0)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/go/src/runtime/asm_amd64.s:2361 (0x456d20)
goexit: BYTE $0x90 // NOP
Someone can tell me what I'm doing wrong or what I'm missing