- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在使用 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")
}
最佳答案
堆栈跟踪是显式的:接口(interface)转换:错误是 *errors.errorString,而不是 *errors.Error
。
所以在checkErr()
中,err
的类型断言是错误的。
解决方法如下:
func checkErr(err error, msg string) {
if err != nil {
log.Fatalln(msg, err.(*errors.errorString).ErrorStack())
}
}
如果您需要了解为什么会出现这种 panic
,我建议您阅读 Golang specs about type assertions .
关于go - 获取/文章/:article_id Not working Gin-Gonic/Gin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30919986/
Doctrine2 和 Symfony: $this->get('database_connection')->executeUpdate("DELETE FROM articles
我正在尝试执行以下查询,但无法找出问题所在。它一直在提示 Unknown column al.article_id in where clause SELECT al.article_id, COUN
我正在使用 API,GET 和 POST 工作正常,除非我尝试通过其 ID 获取选择记录(例如/articles/2)。文章存在,当通过/articles 路径检索所有记录时,我得到了正确的响应。这是
在我决定为评论添加编辑功能之前,我的应用程序运行良好。在此之前,我可以查看特定文章,但现在不能。现在,当我尝试访问有一些评论的特定文章(文章/节目)。实际上,我正在尝试为文章实现评论编辑功能。为此,我
我是一名优秀的程序员,十分优秀!