Skip to content
Back to skills

Cgo Bindings

ASecurity

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.

  • 146 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added October 5, 2026
ai-agentsrustgo

Works with

  • cli

Security analysis

A100/100

Scanned October 5, 2026

npx -y skills add Goldziher/ai-rulez --skill cgo-bindings --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Cgo Bindings?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Cgo Bindings
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/goldziher-cgo-bindings/badge)](https://www.skillsdirectory.com/skills/goldziher-cgo-bindings)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cgo-bindings
description: "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.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.