cgo test dir

Change-Id: Id4c17b85124c40b00e3c88b7d052c2158bfb79a5
This commit is contained in:
Brad Fitzpatrick 2011-07-17 14:03:47 -07:00
parent a558c9ca57
commit 936d9e27c7
6 changed files with 56 additions and 0 deletions

13
misc/cgo/Makefile Normal file
View File

@ -0,0 +1,13 @@
include $(GOROOT)/src/Make.inc
TARG=camdev/ctest
CGOFILES=\
callback.go\
CGO_OFILES=\
callback_c.o\
include $(GOROOT)/src/Make.pkg

22
misc/cgo/callback.go Normal file
View File

@ -0,0 +1,22 @@
package ctest
/*
#include <stdlib.h>
#include "cstuff.h"
*/
import "C"
import "unsafe"
//export GoFoo
func GoFoo(c *C.char) {
gstr := C.GoString(c)
println("I AM GO", gstr)
}
func CallC(s string) {
cstr := C.CString(s)
defer C.free(unsafe.Pointer(cstr))
C.CFoo(cstr)
}

4
misc/cgo/callback_c.c Normal file
View File

@ -0,0 +1,4 @@
int CFoo(const char* c) {
GoFoo(c);
}

1
misc/cgo/cstuff.h Normal file
View File

@ -0,0 +1 @@
extern void CFoo(const char* c);

6
misc/cgo/main.go Normal file
View File

@ -0,0 +1,6 @@
package main
func main() {
CallC()
}

10
misc/cgo/use.go Normal file
View File

@ -0,0 +1,10 @@
package main
import (
"camdev/ctest"
)
func main() {
ctest.CallC("foo")
}