Using the C language code instance in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Transferred from:

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:

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:

2.c.schar (signed Char)
3.c.uchar (unsigned char)
5.c.ushort (unsigned short)
7.c.uint (unsigned int)
9.c.ulong (unsigned long)
10.c.longlong (Long Long)
11.c.ulonglong (unsigned long Long)

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 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 (* //convert a chunk of C memory into a byte array of GolangFunc C.gobytes (unsafe. Pointer, []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, you need to add struct_, Union_, enum_ prefix access in the Golang. Because the data type is not federated in Golang, the Union of C is represented in Golang as a byte array,
3. Those types that are equivalent to C 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: 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.