Use the C language code instance in Golang _golang

Source: Internet
Author: User

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:

Copy Code code as follows:

Package Main

/*
#include <stdio.h>

void Sayhi () {
printf ("Hi");
}
*/
Import "C"

Func Main () {
C.sayhi ()
}

Execution procedure:
Copy Code code as follows:

Go Run main.go

The program executes and outputs hi (more examples can be found $GOROOT/misc/cgo).

Preparations under Windows

If you want to use CGO on Windows, you need to install the GCC compiler, where I use MINGW-W64.

Set up compilation and link flags

We use import "C" to introduce a pseudo package (pseudo-package), through which we 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:

Copy Code code as follows:

#cgo Cflags:-dpng_debug=1
#cgo AMD64 386 cflags:-dx86=1
#cgo Ldflags:-lpng
#include <png.h>
Import "C"

Incidentally, these directives can contain build constraints (builds constraint), as detailed in the following: Http://golang.org/pkg/go/build/.

The commonly used #cgo directives are:

The 1.CPPFLAGS, cflags instruction is used to compile C files in the current package (any. C,. S,. S file)
2.CPPFLAGS, cxxflags directives are used to compile C + + files in the current package (any. cpp,. cc,. cxx files)
The 3.LDFLAGS directive is used to specify the linker flag
The 4.pkg-config directive is used to get compiler and linker flags through the Pkg-config tool (for example: #cgo pkg-config:png Cairo)

Golang Reference C

The points to be noted on the structural body:

1.C structure of the domain name if the Golang keyword, the access needs to be in front of the domain name plus _. For example, there is a struct variable x in C, and there is a field type in the corresponding structure of this variable, then the Type field needs to be accessed through X._type in Golang
2. The structure body's bit field, the misaligned data and so on cannot be expressed in the Golang, will be ignored
C-type fields cannot be used in 3.Golang structures

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 a void function) can return a return value and C's errno variable (as an error):

Copy Code code as follows:

N, Err: = C.sqrt (-1)
_, Err: = C.voidfunc ()

The direct call to the C function pointer is not yet supported.

There are special functions that can be used to convert between C type and Golang type (by copy of data), and the pseudo definition is as follows:

Copy Code code as follows:

Golang string to C string
C's string is allocated using malloc, so the caller of this function
Need to invoke C.free to free memory
Func c.cstring (String) *c.char

Convert C string to Golang string
Func c.gostring (*c.char) string

Converts a certain length of a C string to a Golang string
Func c.gostringn (*c.char, C.int) string

Converts a chunk of C memory to a Golang byte array
Func c.gobytes (unsafe. pointer, c.int) []byte

Other points to note:

The void* in the 1.C language corresponds to unsafe. Pointer
Structures, unions, enum types (not variables) in the 2.C language, and Struct_, Union_, and enum_ prefixes are required to be accessed in Golang. Because this data type is not federated in Golang, the union of C is represented as an array of bytes in Golang
3. Those types that are equivalent to the C language are not exportable

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.