{"id":2323,"library":"tree-sitter-go","title":"Tree-sitter Go Grammar for Python","description":"tree-sitter-go provides Python bindings for the Go programming language grammar, enabling the tree-sitter parsing library to generate incremental syntax trees for Go code. This allows for robust code analysis, highlighting, and manipulation. The library is actively maintained, with releases generally coinciding with updates to the core tree-sitter project.","status":"active","version":"0.25.0","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-go","tags":["parsing","grammar","go","tree-sitter","AST","code analysis","incremental"],"install":[{"cmd":"pip install tree-sitter-go tree-sitter","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required Python version.","package":"python","version":">=3.10","optional":false},{"reason":"Core tree-sitter parsing library bindings required to use the grammar.","package":"tree-sitter","optional":false}],"imports":[{"note":"The `tree_sitter_go` package exports a `language()` function that returns the compiled Go grammar, which is then loaded by the core `tree_sitter.Language` class.","symbol":"language","correct":"import tree_sitter_go as tsgo\nfrom tree_sitter import Language, Parser\n\nGO_LANGUAGE = Language(tsgo.language())"},{"note":"The `Parser` class is part of the core `tree-sitter` Python bindings, not the language-specific grammar package.","wrong":"from tree_sitter_go import Parser","symbol":"Parser","correct":"from tree_sitter import Parser"}],"quickstart":{"code":"import tree_sitter_go as tsgo\nfrom tree_sitter import Language, Parser\n\n# Load the Go language grammar\nGO_LANGUAGE = Language(tsgo.language())\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(GO_LANGUAGE)\n\n# Example Go code (must be bytes for tree-sitter)\ngo_code = b\"\"\"\npackage main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n)\n\nfunc main() {\n\t// A simple 'Hello World' in Go\n\tname := os.Getenv(\"USER_NAME\")\n\tif name == \"\" {\n\t\tname = \"Tree-sitter User\"\n\t}\n\tfm.Println(\"Hello,\", name + \"!\")\n}\n\"\"\"\n\n# Parse the code\ntree = parser.parse(go_code)\n\n# Get the root node of the syntax tree\nroot_node = tree.root_node\n\n# Print basic information about the root node\nprint(f\"Root Node Type: {root_node.type}\")\nprint(f\"Root Node Text (first 50 chars): {root_node.text.decode('utf8')[:50]}...\")\n\n# Traverse and print types of top-level children\nprint(\"\\nTop-level children types:\")\nfor child in root_node.children:\n    print(f\"- {child.type}\")\n","lang":"python","description":"This quickstart demonstrates how to load the Go grammar, create a parser, and parse a simple Go code snippet. It then prints the root node's type and the types of its immediate children, showcasing the basic structure of the generated Abstract Syntax Tree (AST)."},"warnings":[{"fix":"Review the core `tree-sitter` library's `0.25.0` release notes and adjust code for API changes. Ensure your `tree-sitter-go` version is compatible with your `tree-sitter` core version.","message":"The `tree-sitter` core library (a dependency) introduced significant breaking changes in version `0.25.0`, including an ABI bump to 15, removal of `ts_node_child_containing_descendant`, and changes to parsing/query cancellation APIs. Users upgrading the core `tree-sitter` package should consult its changelog for compatibility.","severity":"breaking","affected_versions":"tree-sitter>=0.25.0"},{"fix":"Instead of `Language.build_library`, directly install pre-compiled grammar packages like `tree-sitter-go` via `pip`. The `tree-sitter-go.language()` function already provides the pre-compiled grammar.","message":"The `Language.build_library` static method was removed from the `tree-sitter` Python bindings around version `0.21.x`. Attempting to build grammars dynamically from source within Python code will now fail.","severity":"deprecated","affected_versions":"tree-sitter>=0.21.0"},{"fix":"Ensure you have a compatible C/C++ compiler toolchain installed (e.g., Build Tools for Visual Studio, MinGW, or clang). For many users, installing and using `tree-sitter` within Windows Subsystem for Linux (WSL) can provide a smoother experience.","message":"Users on Windows may encounter challenges when installing `tree-sitter` or language grammars due to underlying C/C++ compilation requirements. Error messages related to missing compilers (e.g., `cl.exe`) are common.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}