Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1026)

Delta Between Two Patch Sets: doc/articles/c_go_cgo.html

Issue 5777054: code review 5777054: doc: add C? Go? Cgo! article (Closed)
Left Patch Set: diff -r 91a86970157c https://code.google.com/p/go Created 13 years ago
Right Patch Set: diff -r e07bc909079c https://code.google.com/p/go Created 13 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | doc/docs.html » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 <!--{ 1 <!--{
2 » "Title": "C? Go? Cgo!", 2 "Title": "C? Go? Cgo!",
3 » "Template": true 3 "Template": true
4 }--> 4 }-->
5 5
6 <p> 6 <p>
7 » Cgo lets Go packages call C code. Given a Go source file written with so me 7 Cgo lets Go packages call C code. Given a Go source file written with some
8 » special features, cgo outputs Go and C files that can be combined into a 8 special features, cgo outputs Go and C files that can be combined into a
9 » single Go package. 9 single Go package.
10 </p> 10 </p>
11 11
12 <p> 12 <p>
13 » To lead with an example, here's a Go package that provides two functions - 13 To lead with an example, here's a Go package that provides two functions -
14 » <code>Random</code> and <code>Seed</code> - that wrap C's <code>random</ code> and <code>srandom</code> functions. 14 <code>Random</code> and <code>Seed</code> - that wrap C's <code>random</code>
15 and <code>srandom</code> functions.
15 </p> 16 </p>
16 17
17 {{code "/doc/progs/cgo1.go" `/package rand/` `/END/`}} 18 {{code "/doc/progs/cgo1.go" `/package rand/` `/END/`}}
18 19
19 <p> 20 <p>
20 » Let’s look at what's happening here, starting with the import statement. 21 Let’s look at what's happening here, starting with the import statement.
21 </p> 22 </p>
22 23
23 <p> 24 <p>
24 » The rand package imports "C", but you'll find there's no such package in 25 The rand package imports "C", but you'll find there's no such package in
25 » the standard Go library. That's because <code>C</code> is a 26 the standard Go library. That's because <code>C</code> is a
26 » "pseudo-package", a special name interpreted by cgo as a reference to C' s 27 "pseudo-package", a special name interpreted by cgo as a reference to C's
27 » name space. 28 name space.
28 </p> 29 </p>
29 30
30 <p> 31 <p>
31 » The rand package contains four references to the <code>C</code> package: 32 The rand package contains four references to the <code>C</code> package:
32 » the calls to <code>C.random</code> and <code>C.srandom</code>, the 33 the calls to <code>C.random</code> and <code>C.srandom</code>, the
33 » conversion <code>C.uint(i)</code>, and the import statement. 34 conversion <code>C.uint(i)</code>, and the import statement.
34 </p> 35 </p>
35 36
36 <p> 37 <p>
37 » The <code>Random</code> function calls the libc random function and retu rns 38 The <code>Random</code> function calls the libc random function and returns
38 » the result. In C, random returns a value of the C type <code>long</code >, 39 the result. In C, random returns a value of the C type <code>long</code>,
39 » which cgo represents as the type <code>C.long</code>. It must be convert ed 40 which cgo represents as the type <code>C.long</code>. It must be converted
40 » to a Go type before it can be used by Go code outside this package, usin g 41 to a Go type before it can be used by Go code outside this package, using
41 » an ordinary Go type conversion: 42 an ordinary Go type conversion:
42 </p> 43 </p>
43 44
44 {{code "/doc/progs/cgo1.go" `/func Random/` `/STOP/`}} 45 {{code "/doc/progs/cgo1.go" `/func Random/` `/STOP/`}}
45 46
46 <p> 47 <p>
47 » Here’s an equivalent function that uses a temporary variable to illustra te 48 Here’s an equivalent function that uses a temporary variable to illustrate
48 » the type conversion more explicitly: 49 the type conversion more explicitly:
49 </p> 50 </p>
50 51
51 {{code "/doc/progs/cgo2.go" `/func Random/` `/STOP/`}} 52 {{code "/doc/progs/cgo2.go" `/func Random/` `/STOP/`}}
52 53
53 <p> 54 <p>
54 » The <code>Seed</code> function does the reverse, in a way. It takes a 55 The <code>Seed</code> function does the reverse, in a way. It takes a
55 » regular Go <code>int</code>, converts it to the C <code>unsigned int</co de> 56 regular Go <code>int</code>, converts it to the C <code>unsigned int</code>
56 » type, and passes it to the C function srandom. 57 type, and passes it to the C function srandom.
57 </p> 58 </p>
58 59
59 {{code "/doc/progs/cgo1.go" `/func Seed/` `/END/`}} 60 {{code "/doc/progs/cgo1.go" `/func Seed/` `/END/`}}
60 61
61 <p> 62 <p>
62 » Note that cgo knows the unsigned int type as C.uint; see the 63 Note that cgo knows the unsigned int type as C.uint; see the
63 » <a href="/cmd/cgo">cgo documentation</a> for a complete list of these 64 <a href="/cmd/cgo">cgo documentation</a> for a complete list of these
64 » numeric type names. 65 numeric type names.
65 </p> 66 </p>
66 67
67 <p> 68 <p>
68 » The one detail of this example we haven't examined yet is the comment 69 The one detail of this example we haven't examined yet is the comment
69 » above the import statement. 70 above the import statement.
70 </p> 71 </p>
71 72
72 {{code "/doc/progs/cgo1.go" `/INCLUDE/` `/STOP/`}} 73 {{code "/doc/progs/cgo1.go" `/INCLUDE/` `/STOP/`}}
73 74
74 <p> 75 <p>
75 » Cgo recognizes this comment and uses it as a header when compiling the C 76 Cgo recognizes this comment and uses it as a header when compiling the C
76 » parts of the package. In this case it is just a simple include statement , 77 parts of the package. In this case it is just a simple include statement,
77 » but it can be any valid C code. The comment must be immediately before t he 78 but it can be any valid C code. The comment must be immediately before the
78 » line that imports "C", without any intervening blank lines, just like a 79 line that imports "C", without any intervening blank lines, just like a
79 » documentation comment. 80 documentation comment.
80 </p> 81 </p>
81 82
82 <p> 83 <p>
83 » <b>Strings and things</b> 84 <b>Strings and things</b>
84 </p> 85 </p>
85 86
86 <p> 87 <p>
87 » Unlike Go, C doesn’t have an explicit string type. Strings in C are 88 Unlike Go, C doesn’t have an explicit string type. Strings in C are
88 » represented by a zero-terminated array of chars. 89 represented by a zero-terminated array of chars.
89 </p> 90 </p>
90 91
91 <p> 92 <p>
92 » Conversion between Go and C strings is done with the 93 Conversion between Go and C strings is done with the
93 » <code>C.CString</code>, <code>C.GoString</code>, and 94 <code>C.CString</code>, <code>C.GoString</code>, and
94 » <code>C.GoStringN</code> functions. These conversions make a copy of the 95 <code>C.GoStringN</code> functions. These conversions make a copy of the
95 » string data. 96 string data.
96 </p> 97 </p>
97 98
98 <p> 99 <p>
99 » This next example implements a <code>Print</code> function that writes a 100 This next example implements a <code>Print</code> function that writes a
100 » string to standard output using C's <code>fputs</code> function from the 101 string to standard output using C's <code>fputs</code> function from the
101 » <code>stdio</code> library: 102 <code>stdio</code> library:
102 </p> 103 </p>
103 104
104 {{code "/doc/progs/cgo3.go" `/package print/` `/END/`}} 105 {{code "/doc/progs/cgo3.go" `/package print/` `/END/`}}
105 106
106 <p> 107 <p>
107 » Memory allocations made by C code are not known to Go's memory manager. 108 Memory allocations made by C code are not known to Go's memory manager.
108 » When you create a C string with <code>C.CString</code> (or any C memory 109 When you create a C string with <code>C.CString</code> (or any C memory
109 » allocation) you must remember to free the memory when you’re done with i t 110 allocation) you must remember to free the memory when you’re done with it
110 » by calling <code>C.free</code>. 111 by calling <code>C.free</code>.
111 </p> 112 </p>
112 113
113 <p> 114 <p>
114 » The call to <code>C.CString</code> returns a pointer to the start of the 115 The call to <code>C.CString</code> returns a pointer to the start of the
115 » char array, so before the function exits we convert it to an 116 char array, so before the function exits we convert it to an
116 » <a href="/pkg/unsafe/#Pointer">unsafe.Pointer</a> and release the memory 117 <a href="/pkg/unsafe/#Pointer">unsafe.Pointer</a> and release the memory
117 » allocation with <code>C.free</code>. A common idiom in cgo programs is t o 118 allocation with <code>C.free</code>. A common idiom in cgo programs is to
118 » <a href="/doc/articles/defer_panic_recover.html">defer</a> the free 119 <a href="/doc/articles/defer_panic_recover.html">defer</a> the free
119 » immediately after allocating (especially when the code that follows is m ore 120 immediately after allocating (especially when the code that follows is more
120 » complex than a single function call), as in this rewrite of 121 complex than a single function call), as in this rewrite of
121 » <code>Print</code>: 122 <code>Print</code>:
122 </p> 123 </p>
123 124
124 {{code "/doc/progs/cgo4.go" `/func Print/` `/END/`}} 125 {{code "/doc/progs/cgo4.go" `/func Print/` `/END/`}}
125 126
126 <p> 127 <p>
127 » <b>Building cgo packages</b> 128 <b>Building cgo packages</b>
128 </p> 129 </p>
129 130
130 <p> 131 <p>
131 » You can use the <a href="/cmd/go/">go tool</a> to build a cgo package: 132 To build cgo packages, just use <a href="/cmd/go/#Compile_packages_and_dependenc ies">"go build"</a> or
132 </p> 133 <a href="/cmd/go/#Compile_and_install_packages_and_dependencies">"go install"</a >
133 134 as usual. The go tool recognizes the special "C" import and automatically uses
134 <pre>go build</pre> 135 cgo for those files.
135
136 <p>
137 » <b>More cgo resources</b>
138 </p> 136 </p>
139 137
140 <p> 138 <p>
141 » The <a href="/cmd/cgo/">cgo command</a> documentation has more detail ab out 139 <b>More cgo resources</b>
142 » the C pseudo-package and the build process. The cgo examples in the Go t ree
143 » demonstrate more advanced concepts.
144 </p> 140 </p>
145 141
146 <p> 142 <p>
147 » For a simple, idiomatic example of a cgo-based package, see Russ Cox’s < a 143 The <a href="/cmd/cgo/">cgo command</a> documentation has more detail about
148 » href="http://code.google.com/p/gosqlite/source/browse/sqlite/sqlite.go"> gosqlite</a>. 144 the C pseudo-package and the build process. The cgo examples in the Go tree
149 » Also, the Go Project Dashboard lists <a 145 demonstrate more advanced concepts.
150 » href="https://godashboard.appspot.com/project?tag=cgo">several other
151 » cgo packages</a>.
152 </p> 146 </p>
153 147
154 <p> 148 <p>
155 » Finally, if you’re curious as to how all this works internally, take a l ook 149 For a simple, idiomatic example of a cgo-based package, see Russ Cox’s <a
156 » at the introductory comment of the runtime package’s <a 150 href="http://code.google.com/p/gosqlite/source/browse/sqlite/sqlite.go">gosqlite </a>.
157 » href="http://code.google.com/p/go/source/browse/src/pkg/runtime/cgocall. c">cgocall.c</a>. 151 Also, the Go Project Dashboard lists <a
152 href="https://godashboard.appspot.com/project?tag=cgo">several other
153 cgo packages</a>.
158 </p> 154 </p>
155
156 <p>
157 Finally, if you’re curious as to how all this works internally, take a look
158 at the introductory comment of the runtime package’s <a href="/src/pkg/runtime/c gocall.c">cgocall.c</a>.
159 </p>
LEFTRIGHT
« no previous file | doc/docs.html » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b