---
source: https://iinventedlight.com/library/notes/go-cgo.md
title: "Call C from Go with cgo and import C"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go docs); BSD-3-Clause (Go source); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Go docs, Command cgo; Go source, cmd/cgo doc.go. BSD-3-Clause; BSD-3-Clause."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Go cgo and the C pseudo-package"
tags: [go, cgo, c, ffi]
client: Librarian
sources:
  - id: S1
    title: "Command cgo"
    publisher: Go docs
    url: https://pkg.go.dev/cmd/cgo
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "cmd/cgo doc.go"
    publisher: Go source
    url: https://raw.githubusercontent.com/golang/go/master/src/cmd/cgo/doc.go
    accessed: 2026-11-19
    license: "BSD-3-Clause"
---
# 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

- [go-modules-basics](https://iinventedlight.com/library/notes/go-modules-basics/)
- [go-error-handling](https://iinventedlight.com/library/notes/go-error-handling/)
- [go-testing](https://iinventedlight.com/library/notes/go-testing/)

## Claims

- id: C1
  claim: Cgo enables the creation of Go packages that call C code.
  source: S1
  evidence: "Cgo enables the creation of Go packages that call C code"

- id: C2
  claim: A comment immediately before import C is a preamble used as a header.
  source: S2
  evidence: "that comment, called the preamble, is used as a header"

- id: C3
  claim: Pseudo #cgo directives can define CFLAGS, CPPFLAGS, CXXFLAGS, FFLAGS, and LDFLAGS.
  source: S2
  evidence: "CFLAGS, CPPFLAGS, CXXFLAGS, FFLAGS and LDFLAGS may be defined"

## Sources

- [S1] Command cgo. Go docs. https://pkg.go.dev/cmd/cgo . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] cmd/cgo doc.go. Go source. https://raw.githubusercontent.com/golang/go/master/src/cmd/cgo/doc.go . License: BSD-3-Clause. Accessed 2026-11-19.

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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
