-
-
Save NickNaso/404b7716ffe29a2224cfb5b1fffac8f6 to your computer and use it in GitHub Desktop.
Examples of calling C code from Golang
This file contains hidden or 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 main | |
//#include<stdio.h> | |
//void inC() { | |
// printf("I am in C code now!\n"); | |
//} | |
import "C" | |
import "fmt" | |
func main() { | |
fmt.Println("I am in Go code now!") | |
C.inC() | |
} |
This file contains hidden or 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<stdio.h> | |
void inCFile() { | |
printf("I am in C code in a .c file now!\n"); | |
} |
This file contains hidden or 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 main | |
/* | |
extern void inCFile(); | |
*/ | |
import "C" | |
import "fmt" | |
func main() { | |
fmt.Println("I am in Go code now!") | |
C.inCFile() | |
} |
This file contains hidden or 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<stdio.h> | |
#include "_cgo_export.h" | |
void inCFile() { | |
printf("I am in C code in a .c file now!\n"); | |
callFromC(); | |
} |
This file contains hidden or 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 main | |
/* | |
#include<stdio.h> | |
extern void inCFile(); | |
*/ | |
import "C" | |
import "fmt" | |
func main() { | |
fmt.Println("I am in Go code now!") | |
C.inCFile() | |
} | |
//export callFromC | |
func callFromC() { | |
fmt.Println("I am in Go code but I was called from C!") | |
} |
This file contains hidden or 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<stdio.h> | |
#include "_cgo_export.h" | |
char * inCFile(char *str) { | |
char *ret = "C String"; | |
printf("Received string from Go: %s\n", str); | |
return ret; | |
} |
This file contains hidden or 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 main | |
/* | |
#include<stdio.h> | |
#include <stdlib.h> | |
extern char * inCFile(char *str); | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"unsafe" | |
) | |
func main() { | |
cstr := C.CString("Go string!") | |
defer C.free(unsafe.Pointer(cstr)) | |
cString := C.inCFile(cstr) | |
gostr := C.GoString(cString) | |
fmt.Println("Received string from C: " + gostr) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment