Naive Go bindings to the C-API of CLang.


As there is no pkg-config entry for clang, you may have to tinker a bit the various CFLAGS and LDFLAGS options, or pass them via the shell:

$ CGO_CFLAGS="-I/somewhere" \
CGO_LDFLAGS="-L/somewhere/else" \
go get


An example on how to use the AST visitor of CLang is provided here:
package main

import (


var fname *string = flag.String("fname", "", "the file to analyze")

func main() {
      fmt.Printf(":: go-clang-dump...\n")
      fmt.Printf(":: fname: %s\n", *fname)
      fmt.Printf(":: args: %v\n", flag.Args())
      if *fname == "" {
              fmt.Printf("please provide a file name to analyze\n")
      idx := clang.NewIndex(0, 1)
      defer idx.Dispose()

      nidx := 0
      args := []string{}
      if len(flag.Args()) > 0 && flag.Args()[0] == "-" {
              nidx = 1
              args = make([]string, len(flag.Args()[nidx:]))
              copy(args, flag.Args()[nidx:])

      tu := idx.Parse(*fname, args, 0)

      defer tu.Dispose()

      fmt.Printf("tu: %s\n", tu.Spelling())
      cursor := tu.ToCursor()
      fmt.Printf("cursor-isnull: %v\n", cursor.IsNull())
      fmt.Printf("cursor: %s\n", cursor.Spelling())
      fmt.Printf("cursor-kind: %s\n", cursor.Kind().Spelling())

      tu_fname := tu.File(*fname).Name()
      fmt.Printf("tu-fname: %s\n", tu_fname)

      fct := func(cursor, parent clang.Cursor) clang.ChildVisitResult {
              if cursor.IsNull() {
                      fmt.Printf("cursor: <none>\n")
                      return clang.CVR_Continue
              fmt.Printf("%s: %s (%s)\n",
                      cursor.Kind().Spelling(), cursor.Spelling(), cursor.USR())
              switch cursor.Kind() {
              case clang.CK_ClassDecl, clang.CK_EnumDecl,
                      clang.CK_StructDecl, clang.CK_Namespace:
                      return clang.CVR_Recurse
              return clang.CVR_Continue


      fmt.Printf(":: bye.\n")

which can be installed like so:

$ go get


  • Only a subset of the C-API of CLang has been provided yet. More will come as patches flow in and time goes by.
  • Go-doc documentation is lagging (but the doxygen docs from the C-API of CLang are in the .go files)