Skip to content

semrekkers/sqlz

Repository files navigation

sqlz

Go Reference build

This package provides a set of helper functions and types to simplify operations with SQL databases in Go. It provides a more flexible and intuitive interface for scanning SQL query results directly into Go structs, slices of structs, or channels of structs. It was inspired by jmoiron/sqlx but it's more lightweight. The core implementation is just the Scan() function.

The package is considered stable and ready for production use.

Examples

Use sqlz to scan a slice of User structs from a query result set.

rows, err := db.QueryContext(ctx, "SELECT * FROM users ORDER BY id LIMIT 10")
if err != nil {
    log.Fatal(err)
}
defer rows.Close()

var records []*User
if err = sqlz.Scan(ctx, rows, &records); err != nil {
    log.Fatal(err)
}
log.Println(records)

It also supports channels:

records := make(chan *User, 8)

go func() {
    defer close(records)
    // ... perform query
    if err = sqlz.Scan(ctx, rows, records); err != nil {
        log.Fatal(err)
    }
}()

for user := range records {
    // Receive all users from the concurrent query.
    fmt.Println("found user:", user.ID)
}

License

MIT License

About

Simple struct/slice/chan mapper for SQL rows.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages