This example demonstrates how to use filesql with sqlx, a popular extension of Go's standard database/sql package.
sqlx provides a set of extensions on Go's standard database/sql library:
- Marshal rows into structs, maps, and slices
- Named parameter support
- Get and Select for convenient querying
- IN query support
sqlx is NOT an ORM - it's a thin wrapper that makes working with SQL more convenient while keeping you in control of your queries.
- Open CSV file with filesql to get
*sql.DB - Wrap it with
sqlx.NewDb(sqlDB, "sqlite3") - Use sqlx's convenient methods (
Get,Select,NamedQuery, etc.)
- Use
db:"xxx"tags to map struct fields to CSV columns - sqlx wraps the existing
*sql.DBwithout creating a new connection - Full access to raw SQL with the convenience of struct scanning
cd examples/sqlx
go run main.go=== All Users ===
ID: 1, Name: Alice, Email: alice@example.com, Age: 28
ID: 2, Name: Bob, Email: bob@example.com, Age: 35
ID: 3, Name: Charlie, Email: charlie@example.com, Age: 22
ID: 4, Name: Diana, Email: diana@example.com, Age: 31
ID: 5, Name: Eve, Email: eve@example.com, Age: 27
=== Get User (ID=1) ===
Found: Alice <alice@example.com>
=== Users Age >= 30 ===
Diana (age 31)
Bob (age 35)
=== Named Query (age >= 25) ===
Alice (age 28)
Bob (age 35)
Diana (age 31)
Eve (age 27)
=== Statistics ===
Total users: 5
Average age: 28.6
go get github.com/jmoiron/sqlx