Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:34:21+00:00 2026-06-12T08:34:21+00:00

I have the following code, and I’m not sure why it is not returning

  • 0

I have the following code, and I’m not sure why it is not returning a slice of Notes. I am using the mgo library from labix.org to connect to MongoDB and following their online documentation.

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(&notes)
    if err != nil {
        panic(iter.Err())
    }

    return notes
}

func main() {
    notes := loadNotes()
    for note := range notes {
        fmt.Println(note.Title)
    }
}  

If I just print out notes I get what looks like a slice of two structs, but I can’t access them by means of notes.Title or anything like that.

[{ Some example title 20 September 2012 Some example content}]

This is what my documents look like:

> db.notes.find()
{ "_id" : "some-example-title", "title" : "Some example title", "date" : "20 September 2012", "body" : "Some example content" }

The real issue is that it’s returning the notes as one big slice rather than a Note{} (I think?)

If I’m doing something obviously wrong, any insight would help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:34:23+00:00Added an answer on June 12, 2026 at 8:34 am

    your problem is here:

    for note := range notes {
        fmt.Println(note.Title)
    }
    

    it should read:

    for _, note := range notes {
        fmt.Println(note.Title)
    }
    

    using a range statement on a slice returns pairs of the form i, v, where i is the index in the slice, and v is the item at the index in that slice. Since you omitted the second value, you’re looping on indexes, not on Note values.

    It’s in the RangeClause section of the spec: http://golang.org/ref/spec#RangeClause

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for loading image from url in xml parsing endElement method
I have following code for inserting data into database using PDO. It inserts data
I have following code using hibernate to throw a custom exception on error and
I have following code.. $query = SELECT quote, author FROM quotes ORDER BY id
I have following code var res = from c in model.Data select new object[]
I have following code import imp from my_module import MyClass import my_module imp.reload(my_module) print(MyClass
I have following code on http://septicsecurity.com/facebook.html It used to work, but does not work
I have following code (modified code from this tutorial ): -(NSMutableArray *) getChampionDatabase {
I have following code import sys from ctypes import * from ctypes.util import find_library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.