Querybag is a library that loads all SQL files from a specific directory, and it allows you to access them by using their file name, as in the example below.
This way you don't have extremely long strings of SQL statements in your code, but you can have them in nice, separate, SQL files instead.
Querybag is licensed under a MIT license.
A simple go get github.com/Machiel/querybag
should suffice.
queries/retrieve_user_by_id.sql
SELECT *
FROM user
WHERE id = ?
package main
import (
"fmt"
"database/sql"
"github.com/Machiel/querybag"
)
var db *sql.DB
func main() {
userID := 12
b, err := querybag.New("queries") // Load all SQL files from queries directory
if err != nil {
panic(err)
}
rows, err := db.Query(b.Get("retrieve_user_by_id"), userID)
// Business as usual
}