In this post, we will see how to pass strings to a C function and receive strings returned from a C function. CGo provides convenient functions C.CString and C.GoString to convert Golang string into C char * and vice versa.
It should be noted that C.CString returns a dynamically allocated char array and should be freed to avoid memory leaks.
If go compiler throws an error like this:
error: ‘free’ undeclared (first use in this function)
It is because you are missing stdlib.h include which provides the free function.
Note: stdlib.h should be included in Go file.