go get -u github.com/go-sqlt/sqlt
sqlt
uses Go’s template engine to create a flexible, powerful, and type-safe SQL builder and struct mapper.
package main
import (
"context"
"database/sql"
"fmt"
"math/big"
"net/url"
"time"
"github.com/go-sqlt/sqlt"
_ "modernc.org/sqlite"
)
type Data struct {
Int int64
String *string
Bool bool
Time time.Time
Big *big.Int
URL *url.URL
IntSlice []int
JSON map[string]any
}
var (
query = sqlt.All[string, Data](sqlt.Parse(`
SELECT
100 {{ Scan "Int" }}
, '200' {{ Scan "String" }}
, true {{ Scan "Bool" }}
, {{ . }} {{ Scan "Time" (ParseTimeInLocation DateOnly UTC) }}
, '300' {{ Scan "Big" UnmarshalText }}
, 'https://example.com/path?query=yes' {{ Scan "URL" UnmarshalBinary }}
, '400,500,600' {{ Scan "IntSlice" (Split "," (ParseInt 10 64)) }}
, '{"hello":"world"}' {{ Scan "JSON" UnmarshalJSON }}
`))
)
func main() {
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
panic(err)
}
data, err := query.Exec(context.Background(), db, time.Now().Format(time.DateOnly))
if err != nil {
panic(err)
}
fmt.Println(data)
// [{100 0x140000116a0 true 2025-05-16 00:00:00 +0000 UTC 300 https://example.com/path?query=yes [400 500 600] map[hello:world]}]
}