OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 /* | 5 /* |
6 | 6 |
7 Cgo enables the creation of Go packages that call C code. | 7 Cgo enables the creation of Go packages that call C code. |
8 | 8 |
9 Usage: cgo [compiler options] file.go | 9 Usage: cgo [compiler options] file.go |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 Within the Go file, C identifiers or field names that are keywords in Go | 46 Within the Go file, C identifiers or field names that are keywords in Go |
47 can be accessed by prefixing them with an underscore: if x points at a C | 47 can be accessed by prefixing them with an underscore: if x points at a C |
48 struct with a field named "type", x._type accesses the field. | 48 struct with a field named "type", x._type accesses the field. |
49 | 49 |
50 The standard C numeric types are available under the names | 50 The standard C numeric types are available under the names |
51 C.char, C.schar (signed char), C.uchar (unsigned char), | 51 C.char, C.schar (signed char), C.uchar (unsigned char), |
52 C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int), | 52 C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int), |
53 C.long, C.ulong (unsigned long), C.longlong (long long), | 53 C.long, C.ulong (unsigned long), C.longlong (long long), |
54 C.ulonglong (unsigned long long), C.float, C.double. | 54 C.ulonglong (unsigned long long), C.float, C.double. |
| 55 The C type void* is represented by Go's unsafe.Pointer. |
55 | 56 |
56 To access a struct, union, or enum type directly, prefix it with | 57 To access a struct, union, or enum type directly, prefix it with |
57 struct_, union_, or enum_, as in C.struct_stat. | 58 struct_, union_, or enum_, as in C.struct_stat. |
58 | 59 |
59 Any C function that returns a value may be called in a multiple | 60 Any C function that returns a value may be called in a multiple |
60 assignment context to retrieve both the return value and the | 61 assignment context to retrieve both the return value and the |
61 C errno variable as an os.Error. For example: | 62 C errno variable as an os.Error. For example: |
62 | 63 |
63 n, err := C.atoi("abc") | 64 n, err := C.atoi("abc") |
64 | 65 |
65 In C, a function argument written as a fixed size array | 66 In C, a function argument written as a fixed size array |
66 actually requires a pointer to the first element of the array. | 67 actually requires a pointer to the first element of the array. |
67 C compilers are aware of this calling convention and adjust | 68 C compilers are aware of this calling convention and adjust |
68 the call accordingly, but Go cannot. In Go, you must pass | 69 the call accordingly, but Go cannot. In Go, you must pass |
69 the pointer to the first element explicitly: C.f(&x[0]). | 70 the pointer to the first element explicitly: C.f(&x[0]). |
70 | 71 |
| 72 A few special functions convert between Go and C types |
| 73 by making copies of the data. In pseudo-Go definitions: |
| 74 |
| 75 // Go string to C string |
| 76 func C.CString(string) *C.char |
| 77 |
| 78 // C string to Go string |
| 79 func C.GoString(*C.char) string |
| 80 |
| 81 // C string, length to Go string |
| 82 func C.GoStringN(*C.char, C.int) string |
| 83 |
| 84 // C pointer, length to Go []byte |
| 85 func C.GoBytes(unsafe.Pointer, C.int) []byte |
| 86 |
71 Cgo transforms the input file into four output files: two Go source | 87 Cgo transforms the input file into four output files: two Go source |
72 files, a C file for 6c (or 8c or 5c), and a C file for gcc. | 88 files, a C file for 6c (or 8c or 5c), and a C file for gcc. |
73 | 89 |
74 The standard package makefile rules in Make.pkg automate the | 90 The standard package makefile rules in Make.pkg automate the |
75 process of using cgo. See $GOROOT/misc/cgo/stdio and | 91 process of using cgo. See $GOROOT/misc/cgo/stdio and |
76 $GOROOT/misc/cgo/gmp for examples. | 92 $GOROOT/misc/cgo/gmp for examples. |
77 | 93 |
78 Cgo does not yet work with gccgo. | 94 Cgo does not yet work with gccgo. |
79 */ | 95 */ |
80 package documentation | 96 package documentation |
OLD | NEW |