Created
September 8, 2016 05:03
-
-
Save LOZORD/04025ac7b605e8c72398f82897eb428c to your computer and use it in GitHub Desktop.
Simple cgo test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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