Skip to content

Instantly share code, notes, and snippets.

@LOZORD
Created September 8, 2016 05:03
Show Gist options
  • Save LOZORD/04025ac7b605e8c72398f82897eb428c to your computer and use it in GitHub Desktop.
Save LOZORD/04025ac7b605e8c72398f82897eb428c to your computer and use it in GitHub Desktop.
Simple cgo test
#include <stdlib.h>
#include <stdio.h>
extern char * GoStrEq(char * cs1, char * st2);
int main(int argc, char ** argv) {
char * s1;
char * s2;
if (argc == 3) {
s1 = argv[1];
s2 = argv[2];
fprintf(stdout, "Got strings: `%s` and `%s`\n", s1, s2);
} else {
fprintf(stderr, "Need exactly 2 strings!\n");
exit(EXIT_FAILURE);
}
char * res = GoStrEq(s1, s2);
fprintf(stdout, "RESULT:\t%s\n", res);
exit(EXIT_SUCCESS);
}
package interop
import "C"
//export GoStrEq
func GoStrEq(cs1, cs2 *C.char) *C.char {
s1, s2 := C.GoString(cs1), C.GoString(cs1)
res := "nada"
if s1 == s2 {
res = s1 + " AND " + s2 + " ARE EQUAL STRINGS!"
} else if s1 < s2 {
res = s1 + " is less than " + s2
} else {
res = s1 + " is greater than " + s2
}
return C.CString(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment