tree-sitter/go-tree-sitter

Go bindings for tree-sitter

C

301

33 commits

updated Nov 16, 2025

See the code

README

Go Tree-sitter

CI Go version Version Docs

This repository contains Go bindings for the Tree-sitter parsing library.

To use this in your Go project, run:

go get github.com/tree-sitter/go-tree-sitter@latest

Example usage:

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())
}

By default, none of the grammars are included in this package. This way, you can only bring in what you need, but it's at the slight cost of having to call go get n times.

In the example above, to fetch the JavaScript grammar, you can run the following:

go get github.com/tree-sitter/tree-sitter-javascript@latest

Alternatively you can also load grammars at runtime from a shared library via purego.

The example below shows how to load the JavaScript grammar from a shared library (libtree-sitter-PARSER_NAME.so) at runtime on Linux & macOS:

For more information on other platforms, see the purego documentation

package main

import (
	tree_sitter "github.com/tree-sitter/go-tree-sitter"
	"github.com/ebitengine/purego"
)

func main() {
	path := "/path/to/your/parser.so"
	lib, err := purego.Dlopen(path, purego.RTLD_NOW|purego.RTLD_GLOBAL)
	if err != nil {
        // handle error
    }

	var javascriptLanguage func() uintptr
	purego.RegisterLibFunc(&javascriptLanguage, lib, "tree_sitter_javascript")

	language := tree_sitter.NewLanguage(unsafe.Pointer(javascriptLanguage()))
}

[!NOTE] Due to bugs with runtime.SetFinalizer and CGO, you must always call Close on an object that allocates memory from C. This must be done for the Parser, Tree, TreeCursor, Query, QueryCursor, and LookaheadIterator objects.

For more information, see the documentation.

tree-sitter/go-tree-sitter

Go bindings for tree-sitter

C

301

33 commits

updated Nov 16, 2025

See the code

README

Go Tree-sitter

CI Go version Version Docs

This repository contains Go bindings for the Tree-sitter parsing library.

To use this in your Go project, run:

go get github.com/tree-sitter/go-tree-sitter@latest

Example usage:

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())
}

By default, none of the grammars are included in this package. This way, you can only bring in what you need, but it's at the slight cost of having to call go get n times.

In the example above, to fetch the JavaScript grammar, you can run the following:

go get github.com/tree-sitter/tree-sitter-javascript@latest

Alternatively you can also load grammars at runtime from a shared library via purego.

The example below shows how to load the JavaScript grammar from a shared library (libtree-sitter-PARSER_NAME.so) at runtime on Linux & macOS:

For more information on other platforms, see the purego documentation

package main

import (
	tree_sitter "github.com/tree-sitter/go-tree-sitter"
	"github.com/ebitengine/purego"
)

func main() {
	path := "/path/to/your/parser.so"
	lib, err := purego.Dlopen(path, purego.RTLD_NOW|purego.RTLD_GLOBAL)
	if err != nil {
        // handle error
    }

	var javascriptLanguage func() uintptr
	purego.RegisterLibFunc(&javascriptLanguage, lib, "tree_sitter_javascript")

	language := tree_sitter.NewLanguage(unsafe.Pointer(javascriptLanguage()))
}

[!NOTE] Due to bugs with runtime.SetFinalizer and CGO, you must always call Close on an object that allocates memory from C. This must be done for the Parser, Tree, TreeCursor, Query, QueryCursor, and LookaheadIterator objects.

For more information, see the documentation.

Significant stargazers

Daniel González Lopes

98 followers · starred Jan 2025

Austin Cawley-Edwards

116 followers · starred Apr 2026

Tushar Sadhwani

364 followers · starred Mar 2025

Konstantin Vyatkin

92 followers · starred Feb 2026