Querying for All Records

Luckily, pulling all records is even easier than pulling one record.

package main

import (
    "bitbucket.org/pop-book/models"
    "fmt"
    "github.com/gobuffalo/pop"
    "log"
)

func main() {
    tx, err := pop.Connect("development")
    if err != nil {
        log.Panic(err)
    }

    users := []models.User{}
    err = tx.All(&users)
    if err != nil {
        fmt.Print("ERROR!\n")
        fmt.Printf("%v\n", err)
    } else {
        fmt.Print("Success!\n")
        fmt.Printf("%v\n", users)
    }
}

We create a slice of the models.User struct, and pass that pointer into the connection's All function. It then gets populated with data, just as with the Find command. Compiling and running will result in

./main
Success!
[{"id":"bf3ba75b-8dfe-4619-b832-31c4a087a589","created_at":"2017-12-19T14:06:20.473953-05:00","updated_at":"2017-12-19T14:06:20.473956-05:00","title":"Ms.","first_name":"Jessica","last_name":"Jones","bio":"Private security, super hero."} {"id":"61b2db41-a66b-4093-8580-be0b114ec04e","created_at":"2017-12-19T14:06:20.476127-05:00","updated_at":"2017-12-19T14:06:20.476128-05:00","title":"Mr.","first_name":"Luke","last_name":"Cage","bio":"Hero for hire."} {"id":"d86df65e-2bf8-4bec-9b23-8d7b274a401a","created_at":"2017-12-19T14:06:20.476837-05:00","updated_at":"2017-12-19T14:06:20.476838-05:00","title":"Mr.","first_name":"Danny","last_name":"Rand","bio":"Martial artist."} {"id":"bd73c1a3-0259-4e0a-a3b2-232cf94b52ff","created_at":"2017-12-19T14:06:20.47763-05:00","updated_at":"2017-12-19T14:06:20.477631-05:00","title":"Mr.","first_name":"Matthew","last_name":"Murdock","bio":"Lawyer, sees with no eyes."} {"id":"240ec3c5-019d-4031-9c27-8a553e022297","created_at":"2017-12-19T14:06:20.478384-05:00","updated_at":"2017-12-19T14:06:20.478385-05:00","title":"Mr.","first_name":"Frank","last_name":"Castle","bio":"USMC, badass."}]

results matching ""

    No results matching ""