Skip to content

Instantly share code, notes, and snippets.

@zelenko
Forked from tejainece/cgo1.go
Created August 11, 2018 02:07
Show Gist options
  • Save zelenko/09ad9b7d8255157122953b612fa2ce25 to your computer and use it in GitHub Desktop.
Save zelenko/09ad9b7d8255157122953b612fa2ce25 to your computer and use it in GitHub Desktop.
Examples of calling C code from Golang
#include<stdio.h>
void inCFile() {
printf("I am in C code in a .c file now!\n");
}
package main
/*
extern void inCFile();
*/
import "C"
import "fmt"
func main() {
fmt.Println("I am in Go code now!")
C.inCFile()
}
#include<stdio.h>
#include "_cgo_export.h"
void inCFile() {
printf("I am in C code in a .c file now!\n");
callFromC();
}
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!")
}
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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment