A lightweight and extensible SQL templating and mapping tool for Go.
- Supports complex SQL template syntax (conditional statements, loops, variable substitution)
- Flexible dynamic condition construction, similar to MyBatis Mapper style
- Lightweight, zero-intrusive, easy to integrate
- Compatible with various database drivers (MySQL, PostgreSQL, SQLite, etc.)
- Provides simple API for quick start
sqlm requires Go version 1.18 or above.
With Go's module support, go [build|run|test] automatically fetches
the necessary dependencies when you add the import in your code:
import "github.com/cavlabs/sqlm"Alternatively, use go get:
go get -u github.com/cavlabs/sqlmA basic example:
package main
import (
"fmt"
"github.com/cavlabs/sqlm"
)
func main() {
// Define a conditional SQL template
tpl := `
SELECT id, name, age FROM users
WHERE 1=1
{{ if .Name }}
AND name = '{{ .Name }}'
{{ end }}
{{ if .MinAge }}
AND age >= {{ .MinAge }}
{{ end }}
`
params := map[string]any{
"Name": "Alice",
"MinAge": 20,
}
sql, err := sqlm.Render(tpl, params)
if err != nil {
panic(err)
}
fmt.Println("Generated SQL:")
fmt.Println(sql)
}- Add SQL syntax parsing optimization
- Support more conditional expressions and functions
- Add SQL pre-compilation and injection protection
- Provide richer examples and plugin system
- Etc.
Contributions are welcome! Please follow the CONTRIBUTING.md guidelines.
MIT License © 2025 cavlabs/sqlm