examples: add a cgo-based package

Change-Id: I602af8071eff4f38bdea1817acbcba98de2720c1
This commit is contained in:
Sebastien Binet 2015-08-10 09:26:06 +02:00
parent d2af1972fd
commit 85958438a3
2 changed files with 49 additions and 0 deletions

28
_examples/cpkg/cpkg.go Normal file
View File

@ -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 <stdio.h>
//#include <string.h>
//#include <stdlib.h>
//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)
}

21
_examples/cpkg/run.go Normal file
View File

@ -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")
}