Transferred from: http://www.jb51.net/article/56720.htm
CGO makes it possible to use C code in Golang.
Hello World
To have a more intuitive understanding, let's look at a simple example of creating a file Main.go:
Package Main /* #include <stdio.h>void sayhi () { printf ("Hi");} */ "C" func Main () { c.sayhi ()}
Execute the program:
Go Run main.go
The program executes and outputs hi (more examples can be seen $GOROOT/misc/cgo).
Preparations under Windows
If you want to use CGO on Windows, then you need to install the GCC compiler, here I use MINGW-W64.
Set compilation and link flags
We imported a pseudo package (Pseudo-package) using the import "C", which we used to use C code. Before import "C", comments that follow the import "C" can include:
1. Compiler and linker flags
2.C Code
We can set compiler and linker flags by #cgo directives, for example:
// #cgo CFLAGS:-dpng_debug=1 // #cgo AMD64 386 CFLAGS:-dx86=1 // #cgo ldflags:-lpng // #include <png.h> " C "
Incidentally, these directives can contain build constraints (build constraint), as detailed in the following: http://golang.org/pkg/go/build/#hdr-build_constraints.
The commonly used #cgo directives are:
The 1.CPPFLAGS, CFLAGS instruction is used to compile the C file in the current package (any. C,. S,. S file)
The 2.CPPFLAGS, cxxflags instruction is used to compile C + + files in the current package (any. cpp,. cc,. cxx files)
The 3.LDFLAGS directive is used to specify linker flags
The 4.pkg-config directive is used to obtain compiler and linker flags through the Pkg-config tool (for example: #cgo pkg-config:png Cairo)
Golang Reference C
Points to note on the structure:
1.C struct domain name if the keyword is Golang, the access needs to precede the domain name with _. For example, there is a struct variable x in C, and there is a field type in the struct that corresponds to this variable, so you need to access the Type field through X._type in Golang.
2. A struct's bit field, non-aligned data, and so on cannot be ignored when represented in Golang
A domain of type C cannot be used in a 3.Golang struct
The standard C numeric type corresponds to:
1.c.char
2.c.schar (signed Char)
3.c.uchar (unsigned char)
4.c.short
5.c.ushort (unsigned short)
6.c.int
7.c.uint (unsigned int)
8.c.long
9.c.ulong (unsigned long)
10.c.longlong (Long Long)
11.c.ulonglong (unsigned long Long)
12.c.float
13.c.double
Any C function (including void functions) can return a return value and C's errno variable (as an error):
N, Err: = C.sqrt (-1) _, err:= C.voidfunc ()
A direct call to a C function pointer is not currently supported.
There are special functions that can be used to convert between C and Golang types (by means of data copies), pseudo-defined as follows:
//Golang string to C string//The string for C is allocated using malloc, so the caller of this function//need to call C.free to free memoryFunc c.cstring (string) *c.Char //convert C string to Golang stringFunc c.gostring (*c.Char)string //converts a certain length of C string to a Golang stringFunc c.gostringn (*c.CharC.int)string //convert a chunk of C memory into a byte array of GolangFunc C.gobytes (unsafe. Pointer, C.int) []byte
Other points to note:
The void* in the 1.C language corresponds to unsafe. Pointer
The structure, Union, enumeration type (not the variable) in the 2.C language requires struct_, union_, enum_ prefix access in the Golang. Because the data type is not federated in Golang, the union of C is represented as a byte array in Golang
3. Those types that are equivalent to the C language are not exportable
Using the C language code instance in Golang