diff --git a/_examples/cpkg/cpkg.go b/_examples/cpkg/cpkg.go new file mode 100644 index 0000000..aaab02d --- /dev/null +++ b/_examples/cpkg/cpkg.go @@ -0,0 +1,28 @@ +// Copyright 2015 The go-python Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpkg + +//#include +//#include +//#include +//void hello(const char *str) { +// fprintf(stdout, str); +//} +import "C" + +import ( + "fmt" + "unsafe" +) + +// Hello prints a string via C's stdio +func Hello(s string) { + if s == "" { + s = "you" + } + cstr := C.CString(fmt.Sprintf("hello %s from C\n", s)) + defer C.free(unsafe.Pointer(cstr)) + C.hello(cstr) +} diff --git a/_examples/cpkg/run.go b/_examples/cpkg/run.go new file mode 100644 index 0000000..85bd647 --- /dev/null +++ b/_examples/cpkg/run.go @@ -0,0 +1,21 @@ +// Copyright 2015 The go-python Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build ignore + +package main + +import ( + "fmt" + + "github.com/go-python/gopy/_examples/cpkg" +) + +func main() { + fmt.Printf("hello from go\n") + cpkg.Hello("me") + fmt.Printf("bye me\n") + cpkg.Hello("you") + fmt.Printf("bye you\n") +}