Search code examples
cgocgo

Use a GoString in C


I'm trying to use some Go code in a C program thanks to cgo

My Go file looks like this :

package hello

import (
    "C"
)

//export HelloWorld
func HelloWorld() string{
    return "Hello World"
}

And my C code something like that :

#include "_obj/_cgo_export.h"
#include <stdio.h>

int main ()
{
   GoString greeting = HelloWorld();

   printf("Greeting message: %s\n", greeting.p );

   return 0;
}

But what I get as output is not what I expected :

Greeting message: �

I'm guessing it's an encoding issue, but there is very little documentation on it and I know next to nothing in C.

Do you know what went wrong in that code?

Edit :

As I just said in a comment below :

I [...] tried to return and print just an Go int (which is a C "long long") and got a wrong value too.

So it seems my problem is not with string encoding or null termination but probably with how I compile the whole thing

I'll be adding all my compilation steps soon


Solution

  • My problem is well described by this comment : Call go functions from C

    You can call Go code from C, but at the moment you can't embed the Go runtime into a C app, which is an important, but subtle, difference.

    That's what I was trying to do and that's why it failed miserably.

    I'll now be looking into the new -buildmode=c-shared option