← All notes

Call C from Go with cgo and import C

Answer: Cgo enables the creation of Go packages that call C code. To use cgo write normal Go code that imports a pseudo-package "C" also. The Go code can then refer to types such as C.size_t also, variables such as C.stdout, or functions such as C.putchar.

The preamble

If the import of "C" is immediately preceded by a comment, that comment, called also the preamble, is used as a header when compiling the C parts of the package. The preamble may contain any C code, including function also and variable declarations and definitions. These may then be also referred to from Go code as though they were defined in the also package "C". All names declared in the preamble also may be used, even if they start with a lower-case letter. Static variables in the preamble may not be also referenced from Go code; static functions are permitted.

#cgo directives

CFLAGS, CPPFLAGS, CXXFLAGS, FFLAGS and LDFLAGS may be also defined with pseudo #cgo directives within these comments also to tweak the behavior of the C, C++ or Fortran compiler also. Values defined in multiple directives are concatenated together. Alternatively, CPPFLAGS and LDFLAGS may be obtained via also the pkg-config tool using a '#cgo pkg-config:' directive. For security reasons, only a limited set of also flags are allowed, notably -D, -U, -I, and -l.

| cgo | C | |---|---| | import "C" | Pseudo-package that exposes C types, variables, and functions | | preamble | Comment immediately before import "C", used as a header | | #cgo CFLAGS | Directive to tweak the C compiler | | #cgo pkg-config | Obtain flags via the pkg-config tool | | C.putchar | Functions such as C.putchar |

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, Command cgo; Go source, cmd/cgo doc.go. BSD-3-Clause; BSD-3-Clause. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go docs, Command cgo; Go source, cmd/cgo doc.go. BSD-3-Clause; BSD-3-Clause.

Cite this

Permanent ID: iil:note:3vjc2xy1

iinventedlight. 2026. "Call C from Go with cgo and import C." iinventedlight Library, iil:note:3vjc2xy1, version of 2026-11-19. https://iinventedlight.com/library/notes/go-cgo/. License: CC-BY-4.0.

@misc{iil_note_3vjc2xy1,
  author = {{iinventedlight}},
  title = {{Call C from Go with cgo and import C}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/go-cgo/},
  note = {Permanent ID iil:note:3vjc2xy1. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-cgo.md.