import ( "fmt" "github.com/cockroachdb/cockroach/sql/parser" ) func main() { stmt, err := parser.ParseOne("SELECT * FROM mytable WHERE name = 'John'") if err != nil { fmt.Println(err) return } sel, ok := stmt.(*parser.Select) if !ok { fmt.Println("not a select statement") return } fmt.Println(sel.From[0].String()) // prints "mytable" }
import ( "github.com/cockroachdb/cockroach/sql/parser" ) func parse(sql string) (string, error) { stmt, err := parser.Parse(sql) if err != nil { return "", err } return stmt.String(), nil }This example provides a function `parse` that takes a SQL statement as a string and returns the string representation of its AST. This package library allows Go developers to easily parse SQL statements into an AST, which can be useful for building SQL-related tools and applications.