Querying for Certain Data

Certain data can be pulled using the Where function. You can even pass in logical operators so long as the database supports in.

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)
    }

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

Here, we use the Where function to narrow down the records we want. Then, we create a slice of the models.User struct, and pass that pointer into the connection's All function just as before. Compiling and running will result in

$ ./main
Success!
[{"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."}]

results matching ""

    No results matching ""