diff --git a/ch2-cgo/ch2-01-hello-cgo.md b/ch2-cgo/ch2-01-hello-cgo.md index 6ef6597..0da467a 100644 --- a/ch2-cgo/ch2-01-hello-cgo.md +++ b/ch2-cgo/ch2-01-hello-cgo.md @@ -86,7 +86,7 @@ package main void cgoPuts(char* s); static void SayHello(const char* s) { - cgoPuts(s); + cgoPuts((char*)(s)); } */ import "C" diff --git a/examples/ch2-01/hello-01/main.go b/examples/ch2-01/hello-01/main.go new file mode 100644 index 0000000..fbfdecd --- /dev/null +++ b/examples/ch2-01/hello-01/main.go @@ -0,0 +1,8 @@ +package main + +//#include +import "C" + +func main() { + C.puts(C.CString("Hello, World\n")) +} diff --git a/examples/ch2-01/hello-02/main.go b/examples/ch2-01/hello-02/main.go new file mode 100644 index 0000000..3c2cd40 --- /dev/null +++ b/examples/ch2-01/hello-02/main.go @@ -0,0 +1,14 @@ +package main + +/* +#include + +static void SayHello(const char* s) { + puts(s); +} +*/ +import "C" + +func main() { + C.SayHello(C.CString("Hello, World\n")) +} diff --git a/examples/ch2-01/hello-03/hello.c b/examples/ch2-01/hello-03/hello.c new file mode 100644 index 0000000..095f0ba --- /dev/null +++ b/examples/ch2-01/hello-03/hello.c @@ -0,0 +1,5 @@ +#include + +void SayHello(const char* s) { + puts(s); +} diff --git a/examples/ch2-01/hello-03/main.go b/examples/ch2-01/hello-03/main.go new file mode 100644 index 0000000..876e944 --- /dev/null +++ b/examples/ch2-01/hello-03/main.go @@ -0,0 +1,8 @@ +package main + +//void SayHello(const char* s); +import "C" + +func main() { + C.SayHello(C.CString("Hello, World\n")) +} diff --git a/examples/ch2-01/hello-04/main.go b/examples/ch2-01/hello-04/main.go new file mode 100644 index 0000000..b59fcc0 --- /dev/null +++ b/examples/ch2-01/hello-04/main.go @@ -0,0 +1,22 @@ +package main + +/* +#include + +void cgoPuts(char* s); + +static void SayHello(const char* s) { + cgoPuts((char*)(s)); +} +*/ +import "C" +import "fmt" + +func main() { + C.SayHello(C.CString("Hello, World\n")) +} + +//export cgoPuts +func cgoPuts(s *C.char) { + fmt.Print(C.GoString(s)) +} diff --git a/examples/ch2-01/hello-05/main.go b/examples/ch2-01/hello-05/main.go new file mode 100644 index 0000000..95ff047 --- /dev/null +++ b/examples/ch2-01/hello-05/main.go @@ -0,0 +1,17 @@ +package main + +//void SayHello(char* s); +import "C" + +import ( + "fmt" +) + +func main() { + C.SayHello(C.CString("Hello, World\n")) +} + +//export SayHello +func SayHello(s *C.char) { + fmt.Print(C.GoString(s)) +}