Use a GoString in C

5k views Asked by At

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

2

There are 2 answers

0
HectorJ On BEST ANSWER

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

1
AudioBubble On

printf expects a NUL-terminated string, but Go strings aren't NUL-terminated, so your C program exhibits undefined behaviour. Do the following instead:

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

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

   char* cGreeting = malloc(greeting.n + 1);
   if (!cGreeting) { /* handle allocation failure */ }
   memcpy(cGreeting, greeting.p, greeting.n);
   cGreeting[greeting.n] = '\0';

   printf("Greeting message: %s\n", cGreeting);

   free(cGreeting);

   return 0;
}

or:

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

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

    printf("Greeting message: ");
    fwrite(greeting.p, 1, greeting.n, stdout);
    printf("\n");

    return 0;
}

or, of course:

func HelloWorld() string {
    return "Hello World\x00"
}