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

iwalkerr · · 3338 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

```go 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 ```

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3338 次点击  ∙  1 赞  
加入收藏 微博
2 回复  |  直到 2019-02-22 09:08:38
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传