go-gin 报错,看了半天没有看出问题

栏目: 数据库 · 发布时间: 5年前

内容简介:///////////////////////////////////////////////////////报错信息/////////////////////////////////////////////////////////////////////////
package main

import (
	"database/sql"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"net/http"
)

type Person struct {
	Id        int    `json:"id" from:"id"`
	FirstName string `json:"first_name" form:"first_name"`
	LastName  string `json:"last_name" form:"last_name"`
}

func init() {
	db, err := sql.Open("mysql", "root:123456@tcp(127.0.0.1)/test_go?parseTime=true")
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()

	db.SetMaxIdleConns(20)
	db.SetMaxOpenConns(20)

	if err := db.Ping(); err != nil {
		log.Fatalln(err)
	}
}

var db sql.DB

func main() {
	router := gin.Default()
	router.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "It works")
	})
	router.GET("/persons/:id", getPersonApi)

	router.Run("127.0.0.1:8000")
}

func getPersonApi(c *gin.Context) {
	id := c.Param("id")
	var person Person
	// 出错地方---------------
	err := db.QueryRow("select id,first_name,last_name from person where id=?", id).Scan(&person.Id, &person.FirstName, &person.LastName)
	if err != nil {
		log.Fatalln(err)
		c.JSON(http.StatusOK, gin.H{
			"person": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"person": person,
	})
}

///////////////////////////////////////////////////////报错信息/////////////////////////////////////////////////////////////////////////

2019/02/22 00:25:03 [Recovery] 2019/02/22 - 00:25:03 panic recovered:
GET /persons/1 HTTP/1.1
Host: localhost:8000
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNTUwOTMxMjA5MDY3Njk2MDAwIiwiaWF0IjoxNTUwNjcyMDA5LCJqdGkiOiJ0b2tlbiJ9.r0a4g5nUr8cttYvuQOSOrVOiuOCObvS-fOE23YK8SQM
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 34
Content-Type: application/json
Cookie: shiro.session=ca6b90a7-5772-49ff-9318-c6367be17494
Postman-Token: 242be282-0dd9-4718-8fbb-23ac504f78c1
User-Agent: PostmanRuntime/7.6.0


runtime error: invalid memory address or nil pointer dereference
/usr/local/Cellar/go/1.11.5/libexec/src/runtime/panic.go:513 (0x102bdc8)
        gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/local/Cellar/go/1.11.5/libexec/src/runtime/panic.go:82 (0x102af1d)
        panicmem: panic(memoryError)
/usr/local/Cellar/go/1.11.5/libexec/src/runtime/signal_unix.go:390 (0x10412e1)
        sigpanic: panicmem()
/usr/local/Cellar/go/1.11.5/libexec/src/database/sql/sql.go:1177 (0x10e0741)
        (*DB).conn: ci, err := db.connector.Connect(ctx)
/usr/local/Cellar/go/1.11.5/libexec/src/database/sql/sql.go:1514 (0x10e3265)
        (*DB).query: dc, err := db.conn(ctx, strategy)
/usr/local/Cellar/go/1.11.5/libexec/src/database/sql/sql.go:1496 (0x10e2f92)
        (*DB).QueryContext: rows, err = db.query(ctx, query, args, cachedOrNewConn)
/usr/local/Cellar/go/1.11.5/libexec/src/database/sql/sql.go:1597 (0x10e3c8c)
        (*DB).QueryRowContext: rows, err := db.QueryContext(ctx, query, args...)
/usr/local/Cellar/go/1.11.5/libexec/src/database/sql/sql.go:1608 (0x10e3dbb)
        (*DB).QueryRow: return db.QueryRowContext(context.Background(), query, args...)
/Users/walker/go/src/gin/test.go:47 (0x159c880)
        getPersonApi: err := db.QueryRow("select id,first_name,last_name from person where id=?", id).Scan(&person.Id, &person.FirstName, &person.LastName)
/Users/walker/go/src/github.com/gin-gonic/gin/context.go:109 (0x1569ee9)
        (*Context).Next: c.handlers[c.index](c)
/Users/walker/go/src/github.com/gin-gonic/gin/recovery.go:76 (0x157c8b9)
        RecoveryWithWriter.func1: c.Next()
/Users/walker/go/src/github.com/gin-gonic/gin/context.go:109 (0x1569ee9)
        (*Context).Next: c.handlers[c.index](c)
/Users/walker/go/src/github.com/gin-gonic/gin/logger.go:176 (0x157bc40)
        LoggerWithConfig.func1: c.Next()
/Users/walker/go/src/github.com/gin-gonic/gin/context.go:109 (0x1569ee9)
        (*Context).Next: c.handlers[c.index](c)
/Users/walker/go/src/github.com/gin-gonic/gin/gin.go:386 (0x157344a)
        (*Engine).handleHTTPRequest: c.Next()
/Users/walker/go/src/github.com/gin-gonic/gin/gin.go:349 (0x1572c91)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/Cellar/go/1.11.5/libexec/src/net/http/server.go:2741 (0x129a27a)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/Cellar/go/1.11.5/libexec/src/net/http/server.go:1847 (0x12964c5)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/Cellar/go/1.11.5/libexec/src/runtime/asm_amd64.s:1333 (0x1058fc0)
        goexit: BYTE    $0x90   // NOP

[GIN] 2019/02/22 - 00:25:03 | 500 |    3.181345ms |       127.0.0.1 | GET      /persons/1

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

具体数学(英文版第2版)

具体数学(英文版第2版)

[美] Ronald L. Graham、Donald E. Knuth、Oren Patashnik / 机械工业出版社 / 2002-8 / 49.00元

This book introduces the mathematics that supports advanced computer Programming and the analysis of algorithms. The primary aim of its well-known authors is to provide a solid and relevant base of ma......一起来看看 《具体数学(英文版第2版)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具