다음 코드를 사용하고 있으며 Notes 슬라이스가 반환되지 않는 이유가 확실하지 않습니다. labix.org의 mgo 라이브러리를 사용하여 MongoDB에 연결하고 온라인 문서를 따르고 있습니다. 난 그냥 notes
을 인쇄 할 경우Go로 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.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{}
보다는 하나 개의 큰 조각으로 노트를 반환하는 점이다 분명히 잘못된 일을하는 것, 어떤 통찰력이 도움이 될 것입니다.
0을 반환하는 값은 무엇입니까? –
^게시물을 좀 더 설명하기 쉽게 만들었습니다. – Zen