这是我的结构定义:
type Article struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Title string `json:"title"`
Author string `json:"author"`
Date string `json:"date"`
Tags string `json:"tags"`
Content string `json:"content"`
Status string `json:"status"`
}
Run Code Online (Sandbox Code Playgroud)
这是我从数据库中获取数据的方法:
func AllArticles() []Article {
articles := []Article{}
err := c_articles.Find(bson.M{}).All(&articles)
if err != nil {
panic(err)
}
return articles
}
Run Code Online (Sandbox Code Playgroud)
这是存储在数据库中的一个对象:
{ "_id" : ObjectId( "5281b83afbb7f35cb62d0834" ),
"title" : "Hello1",
"author" : "DYZ",
"date" : "2013-11-10",
"tags" : "abc",
"content" : "This is another content.",
"status" : "published" }
Run Code Online (Sandbox Code Playgroud)
这是打印结果:
[{ObjectIdHex("") …Run Code Online (Sandbox Code Playgroud) 在Golang中可以为JSON结构标记使用多个名称吗?
type Animation struct {
Name string `json:"name"`
Repeat int `json:"repeat"`
Speed uint `json:"speed"`
Pattern Pattern `json:"pattern",json:"frames"`
}
Run Code Online (Sandbox Code Playgroud)