To parse source code, create a new parser using tree_sitter.NewParser(), set its language using parser.SetLanguage(), and then call parser.Parse().
Important: Because of CGO memory management constraints, you must manually call .Close() on any object that allocates memory from C to avoid leaks. This applies to Parser, Tree, TreeCursor, Query, QueryCursor, and LookaheadIterator.
package main
import (
"fmt"
tree_sitter "github.com/tree-sitter/go-tree-sitter"
tree_sitter_javascript "github.com/tree-sitter/tree-sitter-javascript/bindings/go"
)
func main() {
code := []byte("const foo = 1 + 2")
parser := tree_sitter.NewParser()
defer parser.Close()
parser.SetLanguage(tree_sitter.NewLanguage(tree_sitter_javascript.Language()))
tree := parser.Parse(code, nil)
defer tree.Close()
root := tree.RootNode()
fmt.Println(root.ToSexp())
}