Skip to content

Instantly share code, notes, and snippets.

@surma
Created December 23, 2011 17:08
Show Gist options
  • Save surma/1514784 to your computer and use it in GitHub Desktop.
Save surma/1514784 to your computer and use it in GitHub Desktop.
Cgo Testcase
include $(GOROOT)/src/Make.inc
TARG=testcase
CGOFILES=\
testcase.go\
CGO_OFILES=\
testcase.o\
include $(GOROOT)/src/Make.pkg
#include "testcase.h"
#include "_cgo_export.h"
void CallC(struct MyData *data) {
Callback(data);
}
package testcase
import (
"fmt"
// "unsafe"
)
// #cgo CFLAGS: -I.
// #include "testcase.h"
import "C"
func CallMe() {
data := C.struct_MyData {4}
C.CallC(&data)
}
//export Callback
func Callback(data *C.struct_MyData) {
fmt.Printf("Data: %d\n", data.Number)
}
// // This however works:
// //export Callback
// func Callback(data unsafe.Pointer) {
// fmt.Printf("Data: %d\n", ((*C.struct_MyData)(data)).Number)
// }
#ifndef TESTCASE_H
#define TESTCASE_H
struct MyData {
int Number;
};
void CallC(struct MyData *data);
#endif // TESTCASE_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment