This is a creation in Article, where the information may have evolved or changed.
Cgo allows the Go program to invoke C code. CGO read into a special format to write the Go language source files, output go and C programs, so that the C program can be packaged into the go language package.
For example. Here is a Go Language pack that contains two functions--random and Seed--are vests of the random and srandom functions in the C language library.
PackageRand/ * #include <stdlib.h> * * Import "C"funcRandom() int { return int(C.Random()) }funcSeed(Iint) {C.Srandom(C.UINT(I)) }
Let's take a look at what's going on here. The start is an import statement for a package.
The RAND package imports the "C" package, but you will find that there is no such package in the Go standard library. That's because C is a pseudo-package, a special package name introduced for CGO, which is a reference to the C namespace.
The RAND package contains references to 4 to C packages: Call C.random and C.srandom, type conversion c.uint (i) and a reference statement.
The random function calls the random function in libc and returns the result. In C, random returns a long shape value of type C, which cgo rotates to c.long. This value must be converted to the go type to be used in the GO program. Use a common go-type conversion:
Random ()int { returnint( C . Random ())}
This is an equivalent function that uses a temporary variable for the type conversion:
Random ()int { var r C. Long = C. Random ()returnint(R ) }
The seed function is the opposite. It takes an int of the go language, converts it to the C-unsigned int type, and then passes the Srandom function to C.
Seed (int) { C. Srandom (C. UINT (i)) }
It is important to note that the unsigned int type in CGO is written as C.uint; There is a complete list of types in the CGO documentation.
There is one more detail in this example that we did not mention, which is the comment above the import statement.
/* #include <stdlib.h> * *import"C"
CGO can recognize this comment and treat it as a header file when compiling a C language program. In this case, it is just an include statement, but it can actually be a valid C-language code. This comment must be immediately above the import "C" statement, and cannot have a blank line, just like a document comment.
Strings and Things
Unlike the go language, there is no explicit string type in the C language. A string in C is an array of characters ending in 0.
The string conversions in Go and C are performed through C.cstring, c.gostring, and C.GOSTRINGN functions. These transformations will get a copy of the string type.
The next example is to implement a print function that uses the Fputs function in the C standard library to write a string to the standard output:
Package Print //#include <stdio.h> //#include <stdlib.h> Import "C" Import "unsafe"funcPrint(sstring) {CS:=C.CString(s)C.fputs(CS, (*C.FILE)(C.stdout))C. Free(unsafe.Pointer(CS)) }
Memory allocations in C programs cannot be perceived by the memory manager of the go language. When you use c.cstring to create a C string (or other type of C-language memory allocation), you must remember to use C.free to release it when you are done with it.
Calling C.cstring will return a pointer to the beginning of the character array, so we convert it to an unsafe before the function exits. Pointer (something equivalent to the void of C in Go ), use C.free to release allocated memory. One idiom is to immediately follow a defer after allocating memory (especially when the code is complicated), so we have the following print function:
funcPrint(sstring) {CS:=C.CString(s)defer C. Free(unsafe.Pointer(CS))C.fputs(CS, (*C.FILE)(C.stdout)) }
Build CGO Package
If you use Goinstall, it is easier to build a CGO package, as long as the call uses the Goinstall command as usual, it will automatically recognize this special import "C" and then automatically use CGO to compile the files.
If you want to build using Go makefiles, list the files in the cgofiles variable that you want to work with CGO, just as the gofiles variable contains the normal go source file.
The makefile of the rand package can be written as follows:
include $ (goroot)/ src / Make . Inc Targ = Goblog / Rand Cgofiles =\ Rand . Go \ include $ (goroot)/ src / Make . Pkg
Then enter Gomake to start building.
More Resources for CGO
CGO's documentation contains a more detailed description of the C pseudo-package, as well as a build process. The example of CGO in the Go Code tree gives more advanced usage.
A simple, CGO-based package that fits the go idiom is a gosqlite written by Russ Cox. More CGO packages are also listed on the Go Language website.
Finally, if you're curious about how the CGO inside works, take a look at the notes for the cgocall.c file of the runtime package.
Original link: http://blog.golang.org/2011/03/c-go-cgo.html