March 2020
Intermediate to advanced
406 pages
8h 39m
English
Let's take a look at a relatively straightforward Cgo example. In this example, we will write a simple function to print "Hello Gophers" from a C binding and then we will call that C code from our Go program. In this function, we return a constant character string. We then call the hello_gophers C function within our Go program. We also use the C.GoString function to convert the C string type and the Go string type:
package main/* #include <stdio.h> const char* hello_gophers() { return "Hello Gophers!"; }*/import "C"import "fmt"func main() { fmt.Println(C.GoString(C.hello_gophers()))}
Once this program has been executed, we can see a simple Hello Gophers! output:
This example, while simple, shows us how we can bind C ...
Read now
Unlock full access