SQL Formatter for Go

Free online sql formatter with Go code examples

Working with sql formatter in Go? Our free online sql formatter helps Go developers format, validate, and process data instantly. Below you will find Go code examples using github.com/sqls-server/sqls / custom so you can achieve the same result programmatically in your own projects.

Try the SQL Formatter Online

Use our free SQL Formatter directly in your browser — no setup required.

Open SQL Formatter

Go Code Example

package main

import (
    "fmt"
    "strings"
)

func formatSQL(sql string) string {
    keywords := []string{"SELECT", "FROM", "WHERE", "JOIN", "ON",
        "ORDER BY", "GROUP BY", "HAVING", "LIMIT", "INSERT", "UPDATE",
        "DELETE", "AND", "OR", "LEFT JOIN", "RIGHT JOIN", "INNER JOIN"}

    result := sql
    for _, kw := range keywords {
        result = strings.ReplaceAll(result, " "+kw+" ", "\n"+kw+" ")
        result = strings.ReplaceAll(result, " "+strings.ToLower(kw)+" ", "\n"+strings.ToUpper(kw)+" ")
    }
    return strings.TrimSpace(result)
}

func main() {
    sql := "SELECT u.name, u.email FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100 ORDER BY o.total DESC"
    fmt.Println(formatSQL(sql))
}

Quick Setup

Library: github.com/sqls-server/sqls / custom
go get github.com/kyleconroy/sqlc

Go Tips & Best Practices

  • Go does not have a mainstream SQL formatter library
  • sqlfmt by jackc provides basic SQL formatting
  • For production, consider calling an external formatter

Frequently Asked Questions

SQL Formatter in Other Languages

More Go Tools