mirror of https://github.com/go-python/gopy.git
examples: add a cgo-based package
Change-Id: I602af8071eff4f38bdea1817acbcba98de2720c1
This commit is contained in:
parent
d2af1972fd
commit
85958438a3
|
@ -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)
|
||||
}
|
|
@ -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")
|
||||
}
|
Loading…
Reference in New Issue