Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
d05b3869
Commit
d05b3869
authored
Mar 19, 2012
by
Shenghou Ma
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doc: update format for "C? Go? Cgo!" article
R=adg CC=golang-dev
https://golang.org/cl/5841050
parent
2ef4a840
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
31 deletions
+30
-31
c_go_cgo.html
doc/articles/c_go_cgo.html
+30
-29
cgo1.go
doc/progs/cgo1.go
+0
-2
No files found.
doc/articles/c_go_cgo.html
View file @
d05b3869
...
...
@@ -22,24 +22,24 @@ Let’s look at what's happening here, starting with the import statement.
</p>
<p>
The
rand package imports "C", but you'll find there's no such package in
the standard Go library. That's because
<code>
C
</code>
is a
The
<code>
rand
</code>
package imports
<code>
"C"
</code>
, but you'll find there's
no such package in
the standard Go library. That's because
<code>
C
</code>
is a
"pseudo-package", a special name interpreted by cgo as a reference to C's
name space.
</p>
<p>
The
rand package contains four references to the
<code>
C
</code>
package:
the calls to
<code>
C.random
</code>
and
<code>
C.srandom
</code>
, the
conversion
<code>
C.uint(i)
</code>
, and the
import
statement.
The
<code>
rand
</code>
package contains four references to the
<code>
C
</code>
package:
the calls to
<code>
C.random
</code>
and
<code>
C.srandom
</code>
, the
conversion
<code>
C.uint(i)
</code>
, and the
<code>
import
</code>
statement.
</p>
<p>
The
<code>
Random
</code>
function calls the
libc random function and returns
the result. In C, random returns a value of the C type
<code>
long
</code>
,
which cgo represents as the type
<code>
C.long
</code>
. It must be converted
to a Go type before it can be used by Go code outside this package, using
an ordinary Go type conversion:
The
<code>
Random
</code>
function calls the
standard C library's
<code>
random
</code>
function and returns the result. In C,
<code>
random
</code>
returns a value of the
C type
<code>
long
</code>
, which cgo represents as the type
<code>
C.long
</code>
.
It must be converted to a Go type before it can be used by Go code outside this
package, using
an ordinary Go type conversion:
</p>
{{code "/doc/progs/cgo1.go" `/func Random/` `/STOP/`}}
...
...
@@ -54,30 +54,30 @@ the type conversion more explicitly:
<p>
The
<code>
Seed
</code>
function does the reverse, in a way. It takes a
regular Go
<code>
int
</code>
, converts it to the C
<code>
unsigned int
</code>
type, and passes it to the C function
srandom
.
type, and passes it to the C function
<code>
srandom
</code>
.
</p>
{{code "/doc/progs/cgo1.go" `/func Seed/` `/END/`}}
<p>
Note that cgo knows the
unsigned int type as C.uint; see the
<a
href=
"/cmd/cgo"
>
cgo documentation
</a>
for a complete list of these
numeric type names.
Note that cgo knows the
<code>
unsigned int
</code>
type as
<code>
C.uint
</code>
;
see the
<a
href=
"/cmd/cgo"
>
cgo documentation
</a>
for a complete list of
these
numeric type names.
</p>
<p>
The one detail of this example we haven't examined yet is the comment
above the
import
statement.
above the
<code>
import
</code>
statement.
</p>
{{code "/doc/progs/cgo1.go" `/
INCLUDE
/` `/STOP/`}}
{{code "/doc/progs/cgo1.go" `/
\/\*
/` `/STOP/`}}
<p>
Cgo recognizes this comment and uses it as a header when compiling the C
parts of the package. In this case it is just a simple include statement,
but it can be any valid C code. The comment must be immediately before the
line that imports
"C", without any intervening blank lines, just like a
documentation comment.
line that imports
<code>
"C"
</code>
, without any intervening blank lines,
just like a
documentation comment.
</p>
<p>
...
...
@@ -114,11 +114,11 @@ by calling <code>C.free</code>.
<p>
The call to
<code>
C.CString
</code>
returns a pointer to the start of the
char array, so before the function exits we convert it to an
<a
href=
"/pkg/unsafe/#Pointer"
>
unsafe.Pointer
</a>
and release the memory
allocation with
<code>
C.free
</code>
. A common idiom in cgo programs is to
<a
href=
"/doc/articles/defer_panic_recover.html"
>
defer
</a>
the free
immediately after allocating (especially when the code that follows is more
complex than a single function call), as in this rewrite of
<a
href=
"/pkg/unsafe/#Pointer"
>
<code>
unsafe.Pointer
</code></a>
and release
the memory allocation with
<code>
C.free
</code>
. A common idiom in cgo programs
is to
<a
href=
"/doc/articles/defer_panic_recover.html"
><code>
defer
</code></a>
the free immediately after allocating (especially when the code that follows
is more
complex than a single function call), as in this rewrite of
<code>
Print
</code>
:
</p>
...
...
@@ -129,10 +129,11 @@ complex than a single function call), as in this rewrite of
</p>
<p>
To build cgo packages, just use
<a
href=
"/cmd/go/#Compile_packages_and_dependencies"
>
"go build"
</a>
or
<a
href=
"/cmd/go/#Compile_and_install_packages_and_dependencies"
>
"go install"
</a>
as usual. The go tool recognizes the special "C" import and automatically uses
cgo for those files.
To build cgo packages, just use
<a
href=
"/cmd/go/#Compile_packages_and_dependencies"
>
"
<code>
go build
</code>
"
</a>
or
<a
href=
"/cmd/go/#Compile_and_install_packages_and_dependencies"
>
"
<code>
go install
</code>
"
</a>
as usual. The go tool recognizes the special
<code>
"C"
</code>
import and automatically
uses cgo for those files.
</p>
<p>
...
...
@@ -141,8 +142,8 @@ cgo for those files.
<p>
The
<a
href=
"/cmd/cgo/"
>
cgo command
</a>
documentation has more detail about
the C pseudo-package and the build process. The
cgo examples in the Go tree
demonstrate more advanced concepts.
the C pseudo-package and the build process. The
<a
href=
"/misc/cgo/"
>
cgo examples
</a>
in the Go tree
demonstrate more advanced concepts.
</p>
<p>
...
...
doc/progs/cgo1.go
View file @
d05b3869
...
...
@@ -3,8 +3,6 @@
// license that can be found in the LICENSE file.
package
rand
// INCLUDE OMIT
/*
#include <stdlib.h>
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment