aboutsummaryrefslogtreecommitdiff
path: root/content/c-go-cgo.article
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2020-03-09 23:54:35 -0400
committerRuss Cox <rsc@golang.org>2020-03-17 20:58:37 +0000
commitaf5018f64e406aaa646dae066f28de57321ea5ce (patch)
tree8db7b1f049d83d215fa9abf68851efce7b5ccadb /content/c-go-cgo.article
parent86e424fac66fa90ddcb7e8d7febd4c2b07d7c59e (diff)
content: convert to Markdown-enabled present inputs
Converted blog to Markdown-enabled present (CL 222846) using present2md (CL 222847). For golang/go#33955. Change-Id: Ib39fa1ddd9a46f9c7a62a2ca7b96e117635553e8 Reviewed-on: https://go-review.googlesource.com/c/blog/+/222848 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Andrew Bonventre <andybons@golang.org>
Diffstat (limited to 'content/c-go-cgo.article')
-rw-r--r--content/c-go-cgo.article37
1 files changed, 19 insertions, 18 deletions
diff --git a/content/c-go-cgo.article b/content/c-go-cgo.article
index 2ee6e92..912ed32 100644
--- a/content/c-go-cgo.article
+++ b/content/c-go-cgo.article
@@ -1,10 +1,11 @@
-C? Go? Cgo!
+# C? Go? Cgo!
17 Mar 2011
Tags: cgo, technical
+Summary: Cgo lets Go packages call C code. Given a Go source file written with some special features, cgo outputs Go and C files that can be combined into a single Go package.
Andrew Gerrand
-* Introduction
+## Introduction
Cgo lets Go packages call C code. Given a Go source file written with some special features,
cgo outputs Go and C files that can be combined into a single Go package.
@@ -56,15 +57,15 @@ Here's an equivalent function that uses a temporary variable to illustrate the t
}
The `Seed` function does the reverse, in a way.
-It takes a regular Go `int`, converts it to the C `unsigned`int` type,
+It takes a regular Go `int`, converts it to the C `unsigned int` type,
and passes it to the C function `srandom`.
func Seed(i int) {
C.srandom(C.uint(i))
}
-Note that cgo knows the `unsigned`int` type as `C.uint`;
-see the [[https://golang.org/cmd/cgo][cgo documentation]] for a complete
+Note that cgo knows the `unsigned int` type as `C.uint`;
+see the [cgo documentation](https://golang.org/cmd/cgo) for a complete
list of these numeric type names.
The one detail of this example we haven't examined yet is the comment above the `import` statement.
@@ -84,13 +85,13 @@ The `#cgo` directives are used to provide flags for the compiler and linker
when building the C parts of the package.
There is a limitation: if your program uses any `//export` directives,
-then the C code in the comment may only include declarations (`extern`int`f();`),
-not definitions (`int`f()`{`return`1;`}`).
+then the C code in the comment may only include declarations (`extern int f();`),
+not definitions (`int f() { return 1; }`).
You can use `//export` directives to make Go functions accessible to C code.
-The `#cgo` and `//export` directives are documented in the [[https://golang.org/cmd/cgo/][cgo documentation]].
+The `#cgo` and `//export` directives are documented in the [cgo documentation](https://golang.org/cmd/cgo/).
-* Strings and things
+## Strings and things
Unlike Go, C doesn't have an explicit string type. Strings in C are represented by a zero-terminated array of chars.
@@ -119,9 +120,9 @@ When you create a C string with `C.CString` (or any C memory allocation)
you must remember to free the memory when you're done with it by calling `C.free`.
The call to `C.CString` returns a pointer to the start of the char array,
-so before the function exits we convert it to an [[https://golang.org/pkg/unsafe/#Pointer][`unsafe.Pointer`]]
+so before the function exits we convert it to an [`unsafe.Pointer`](https://golang.org/pkg/unsafe/#Pointer)
and release the memory allocation with `C.free`.
-A common idiom in cgo programs is to [[https://golang.org/doc/articles/defer_panic_recover.html][`defer`]]
+A common idiom in cgo programs is to [`defer`](https://golang.org/doc/articles/defer_panic_recover.html)
the free immediately after allocating (especially when the code that follows
is more complex than a single function call),
as in this rewrite of `Print`:
@@ -132,18 +133,18 @@ as in this rewrite of `Print`:
C.fputs(cs, (*C.FILE)(C.stdout))
}
-* Building cgo packages
+## Building cgo packages
-To build cgo packages, just use [[https://golang.org/cmd/go/#Compile_packages_and_dependencies][`go`build`]]
-or [[https://golang.org/cmd/go/#Compile_and_install_packages_and_dependencies][`go`install`]] as usual.
+To build cgo packages, just use [`go build`](https://golang.org/cmd/go/#Compile_packages_and_dependencies)
+or [`go install`](https://golang.org/cmd/go/#Compile_and_install_packages_and_dependencies) as usual.
The go tool recognizes the special `"C"` import and automatically uses cgo for those files.
-* More cgo resources
+## More cgo resources
-The [[https://golang.org/cmd/cgo/][cgo command]] documentation has more
+The [cgo command](https://golang.org/cmd/cgo/) documentation has more
detail about the C pseudo-package and the build process.
-The [[https://golang.org/misc/cgo/][cgo examples]] in the Go tree demonstrate
+The [cgo examples](https://golang.org/misc/cgo/) in the Go tree demonstrate
more advanced concepts.
Finally, if you're curious as to how all this works internally,
-take a look at the introductory comment of the runtime package's [[https://golang.org/src/runtime/cgocall.go][cgocall.go]].
+take a look at the introductory comment of the runtime package's [cgocall.go](https://golang.org/src/runtime/cgocall.go).