2

我正在研究一个 API,GET 和 POST 工作正常,除非我尝试通过其 ID 获取选择记录(例如 /articles/2)。该文章存在,当通过 /articles 路由检索所有记录时,我得到了正确的响应。这是堆栈跟踪。

    $ go run main.go
    [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
    - using env:   export GIN_MODE=release
    - using code:  gin.SetMode(gin.ReleaseMode)

    [GIN-debug] GET   /                         --> main.index (3 handlers)
    [GIN-debug] GET   /articles                 --> main.ArticlesList (3 handlers)
    [GIN-debug] POST  /articles                 --> main.ArticlePost (3 handlers)
    [GIN-debug] GET   /articles/:article_id     --> main.ArticlesDetail (3 handlers)
    [GIN-debug] Listening and serving HTTP on :8000
    2015/06/18 10:31:49 Panic recovery -> interface conversion: error is *errors.errorString, not *errors.Error
    c:/go/src/runtime/panic.go:387 (0x4114b6)
            gopanic: reflectcall(unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
    c:/go/src/runtime/iface.go:181 (0x40ae1a)
            assertI2T: panic(&TypeAssertionError{*tab.inter.typ._string, *tab._type._string, *t._string, ""})
    c:/Users/Fresh/go/src/github.com/jisaw/portfolio-server/main.go:37 (0x401261)
            checkErr: log.Fatalln(msg, err.(*errors.Error).ErrorStack())
    c:/Users/Fresh/go/src/github.com/jisaw/portfolio-server/main.go:97 (0x401e2c)
            getArticle: checkErr(err, "selectOne failed")
    c:/Users/Fresh/go/src/github.com/jisaw/portfolio-server/main.go:60 (0x4016a7)
            ArticlesDetail: article := getArticle(a_id)
    c:/Users/Fresh/go/src/github.com/gin-gonic/gin/context.go:86 (0x43694a)
            (*Context).Next: c.handlers[c.index](c)
    c:/Users/Fresh/go/src/github.com/gin-gonic/gin/logger.go:56 (0x442ff0)
            func.007: c.Next()
    c:/Users/Fresh/go/src/github.com/gin-gonic/gin/context.go:86 (0x43694a)
            (*Context).Next: c.handlers[c.index](c)
    c:/Users/Fresh/go/src/github.com/gin-gonic/gin/recovery.go:43 (0x4437e0)
            func.009: c.Next()
    c:/Users/Fresh/go/src/github.com/gin-gonic/gin/context.go:86 (0x43694a)
            (*Context).Next: c.handlers[c.index](c)
    c:/Users/Fresh/go/src/github.com/gin-gonic/gin/gin.go:249 (0x43b275)
            (*Engine).handleHTTPRequest: context.Next()
    c:/Users/Fresh/go/src/github.com/gin-gonic/gin/gin.go:230 (0x43aff9)
            (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
    c:/go/src/net/http/server.go:1703 (0x4ad385)
            serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
    c:/go/src/net/http/server.go:1204 (0x4ab378)
            (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
    c:/go/src/runtime/asm_386.s:2287 (0x435c01)
            goexit:

和代码

package main

import (
    "github.com/gin-gonic/gin"
    "database/sql"
    "github.com/coopernurse/gorp"
    _ "github.com/mattn/go-sqlite3"
    "log"
    "time"
    "strconv"
    "github.com/go-errors/errors"
)

type Article struct {
    Id int64 `db:"article_id"`
    Created int64
    Title string
    Content string
}

var dbmap = initDb()

func initDb() gorp.DbMap {
    db, err := sql.Open("sqlite3", "db.sqlite3")
    checkErr(err, "sql.Open faild")
    dbmap := gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
    dbmap.AddTableWithName(Article{}, "articles").SetKeys(true, "Id")
    err = dbmap.CreateTablesIfNotExists()
    checkErr(err, "Create tables failed")
    return dbmap
}

func checkErr(err error, msg string) {
    if err != nil {
        log.Fatalln(msg, err.(*errors.Error).ErrorStack())
    }
}

func index (c *gin.Context) {
    content := gin.H{"Hello": "World"}
    c.JSON(200, content)
}

func ArticlesList(c *gin.Context) {
    var articles []Article
    _, err := dbmap.Select(&articles, "select * from articles order by article_id")
    checkErr(err, "Select failed")
    content := gin.H{}
    for k, v := range articles {
        content[strconv.Itoa(k)] =v
    }
    c.JSON(200, content)
}

func ArticlesDetail(c *gin.Context) {
    article_id := c.Params.ByName("id")
    a_id, _ := strconv.Atoi(article_id)
    article := getArticle(a_id)
    content := gin.H{"title": article.Title, "content": article.Content}
    c.JSON(200, content)
}

func ArticlePost(c *gin.Context) {
    var json Article

    c.Bind(&json)
    article := createArticle(json.Title, json.Content)
    if article.Title == json.Title {
        content := gin.H{
            "result": "Success",
            "title": article.Title,
            "content": article.Content,
        }
        c.JSON(201, content)
    } else {
        c.JSON(500, gin.H{"result": "An error occured"})
    }
}

func createArticle(title, body string) Article {
    article := Article{
        Created: time.Now().UnixNano(),
        Title: title,
        Content: body,
    }

    err := dbmap.Insert(&article)
    checkErr(err, "Insert failed")
    return article
}

func getArticle(article_id int) Article {
    article := Article{}
    err := dbmap.SelectOne(&article, "select * from articles where article_id=?", article_id)
    checkErr(err, "selectOne failed")
    return article
}

func main() {
    app := gin.Default()
    app.GET("/", index)
    app.GET("/articles", ArticlesList)
    app.POST("/articles", ArticlePost)
    app.GET("/articles/:article_id", ArticlesDetail)
    app.Run(":8000")
}
4

1 回答 1

2

堆栈跟踪是明确的:interface conversion: error is *errors.errorString, not *errors.Error.

所以在 中checkErr(),类型断言err是错误的。

这是解决方案:

func checkErr(err error, msg string) {
    if err != nil {
        log.Fatalln(msg, err.(*errors.errorString).ErrorStack())
    }
}

如果您需要了解为什么会这样panic,我建议您阅读有关类型断言的 Golang 规范

于 2015-06-18T15:57:42.303 回答