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

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

内容简介:///////////////////////////////////////////////////////报错信息/////////////////////////////////////////////////////////////////////////
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

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

查看所有标签

猜你喜欢:

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

数据结构与算法

数据结构与算法

BrunoRPreiss / 电子工业出版社 / 2003-1 / 55.00元

本书是作者根据他在滑铁卢大学计算机工程学院教授数据结构与算法课程的经验编写而成的。它采用C++面向对象的设计模式,不仅系统全面地介绍了各种传统的数据结构,还把它们按照类和类层次的现代理念予以展开,进而达到抽象结构与实际设计的完美统一。本书的后三章通过引入抽象问题求解的概念,集中讲述了算法技术和各算法之间的关系。另外,作者运用一定的数学工具以及必要的分析技术和分析理论,对每种数据结构及相关算法都进行一起来看看 《数据结构与算法》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具