3

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

HectorJ
  • 5,814
  • 3
  • 34
  • 52
  • 2
    Looks as if the Go-string is not NUL-terminated. There might be a length in the structure and you have to `memcpy()` to a buffer and append `\0` yourself? Or there is a conversion function which does that? – too honest for this site Jun 18 '15 at 13:53
  • There is a length indeed, GoString is defined as "typedef struct { char *p; GoInt n; } GoString;" I'll try to dig this way, thanks – HectorJ Jun 18 '15 at 13:55
  • `write(STDOUT_FILENO, greeting.p, n)` should do the work (if you don't want to `memcpy` the buffer). – nouney Jun 18 '15 at 15:13

2 Answers2

2

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"
}
  • Hi! Thanks for your answer. I tried all code samples with no success. I then 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 – HectorJ Jun 20 '15 at 12:37
0

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

HectorJ
  • 5,814
  • 3
  • 34
  • 52