私は次のコードを持っていますが、なぜそれがNotesのスライスを返さないのかわかりません。labix.orgのmgoライブラリを使用してMongoDBに接続し、オンラインドキュメントに従っています。
type Note struct {
Url string
Title string
Date string
Body string
}
func loadNotes() ([]Note) {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("notes")
notes := []Note{}
iter := c.Find(nil).Limit(100).Iter()
err = iter.All(¬es)
if err != nil {
panic(iter.Err())
}
return notes
}
func main() {
notes := loadNotes()
for note := range notes {
fmt.Println(note.Title)
}
}
印刷するだけnotes
で、2つの構造体のスライスのように見えますが、そのような方法でそれらにアクセスすることはできませんnotes.Title
。
[{ Some example title 20 September 2012 Some example content}]
これは私のドキュメントがどのように見えるかです:
> db.notes.find()
{ "_id" : "some-example-title", "title" : "Some example title", "date" : "20 September 2012", "body" : "Some example content" }
Note{}
本当の問題は、ノートを(私が思うに)ではなく、1つの大きなスライスとして返すことです。
私が明らかに間違ったことをしているなら、どんな洞察も助けになるでしょう。