- Use
import "C"with// #cgo LDFLAGS:and// #cgo CFLAGS:directives for linking. - Declare C header functions in comment block immediately above
import "C"— no blank line between. - Type mapping:
C.int,C.long,C.char. UseC.CString()andC.GoString()for string conversion. - Memory:
C.CString()allocates — alwaysdefer C.free(unsafe.Pointer(cStr))immediately after creation. - Error handling: return error codes from C functions, convert to Go
errorvalues. Check all return values. - Link Rust static libraries:
// #cgo LDFLAGS: -L./target/release -lmylib. Build Rust withcrate-type = ["staticlib"]. - Thread safety: C functions called from Go may run on any OS thread. Use
runtime.LockOSThread()when needed. - Go pointer passing: use
runtime.Pinner(Go 1.21+) to pin Go memory passed to C. Never pass Go pointers without pinning. - Test with
go test(requiresCGO_ENABLED=1). Mock C layer for unit tests, use integration tests for real FFI. - Build:
CGO_ENABLED=1 go build. Cross-compilation requires appropriate C toolchain. - Anti-patterns: leaking
C.CStringmemory, passing unpinned Go pointers to C, ignoring C return codes.
Cgo Bindings
cgo conventions for linking Go to a C or Rust static library: import "C" directives, type/string conversion, CString memory management, thread safety, and Go pointer pinning. Load when generating or reviewing cgo bindings that call a C or Rust core from Go.
Cgo Bindings by goldziher · 1fe8361
npx skillmds@latest add goldziher/cgo-bindings File contents
---name: cgo-bindingsdescription: cgo conventions for linking Go to a C or Rust static library: import "C" directives, type/string conversion, CString memory management, thread safety, and Go pointer pinning. Load when generating or reviewing cgo bindings that call a C or Rust core from Go.---- Use `import "C"` with `// #cgo LDFLAGS:` and `// #cgo CFLAGS:` directives for linking.- Declare C header functions in comment block immediately above `import "C"` — no blank line between.- Type mapping: `C.int`, `C.long`, `C.char`. Use `C.CString()` and `C.GoString()` for string conversion.- Memory: `C.CString()` allocates — always `defer C.free(unsafe.Pointer(cStr))` immediately after creation.- Error handling: return error codes from C functions, convert to Go `error` values. Check all return values.- Link Rust static libraries: `// #cgo LDFLAGS: -L./target/release -lmylib`. Build Rust with `crate-type = ["staticlib"]`.- Thread safety: C functions called from Go may run on any OS thread. Use `runtime.LockOSThread()` when needed.- Go pointer passing: use `runtime.Pinner` (Go 1.21+) to pin Go memory passed to C. Never pass Go pointers without pinning.- Test with `go test` (requires `CGO_ENABLED=1`). Mock C layer for unit tests, use integration tests for real FFI.- Build: `CGO_ENABLED=1 go build`. Cross-compilation requires appropriate C toolchain.- Anti-patterns: leaking `C.CString` memory, passing unpinned Go pointers to C, ignoring C return codes.
goldziher/ai-rulez/tree/main/internal/builtins/bindings/cgo/skills/cgo-bindings commit 1fe8361d33
Frequently asked questions
Run npx skillmds@latest add goldziher/cgo-bindings in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
cgo conventions for linking Go to a C or Rust static library: import "C" directives, type/string conversion, CString memory management, thread safety, and Go pointer pinning. Load when generating or reviewing cgo bindings that call a C or Rust core from Go. It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
goldziher (@goldziher) published this skill. Their other Agent Skills are listed on their SkillMD profile.