Call C function from Go!
August 2022
450 Words, 3 Minutes
CGO
is one of the powerful features that golang provides, which helps us to embed C
applications with GO
.
This becomes possible with the help of pseudo-package C
. C
is actually a foreign function interface that assists the C function calls from Go.
It would be much easier to understand this with an example, so let’s just dive right into it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
// #include "area.c"
import "C"
import (
"errors"
"fmt"
"log"
)
func Area(x, y int) (int, error) {
width := C.int(x)
height := C.int(y)
area, err := C.Area(width, height)
if err != nil {
return 0, errors.New("error calling Area Function: " + err.Error())
}
result := int(area)
return result, nil
}
func main() {
area, err := Area(25, 20)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Area is %d\n", area)
}
//=> prints 'Area is 500'.
The above main.go file contains a lot of information, so let’s break it down. Line No. 4 is where we import the C
FFI so that we can call our C code. This line is preceded by #include "area.c"
, this statement is called a preamble
. This preamble is used as a header while compiling C
parts of the program. The code inside area.c
looks as follows:
1
2
3
4
5
#include<stdio.h>
int Area(int x, int y){
return x*y;
}
Getting back to our main.go file, line no. 40 invokes the function Area
that takes two go integers. The first step in this function i.e on line 26-27 is to type cast the go integers to C ints. Different data types in golang needs to be type casted to its corresponding data type in C. After that we call the C.Area
function that is defined in our area.c
file. The actual function only returns the product of two values but in Go we have two return values area and err
. The err is not nil if we have some errors in our C code. This provides us a better way to handle errors from the golang side.
After we get the result all we need to do is convert the data type of the result to corresponding Go data type(line no. 34).
Finally we can go ahead and build the code using go build
, which gives us the following output:
Area is 500
That’s it, we are able to successfully call our C code from Golang side.
References: cgo-docs