内容简介:///////////////////////////////////////////////////////报错信息/////////////////////////////////////////////////////////////////////////
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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 不用挨个数数,DNN也能「一眼看出」目标数量
- 0615 - 没有比较,就没有伤害
- 对话微软ONNX负责人:没有中国的认可,开源标准没有意义
- 建模的世界没有银弹
- 私有链可能没有未来?
- Java 并没有死!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。