2015-08-10 07:26:06 +00:00
|
|
|
// 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>
|
2015-08-10 07:41:44 +00:00
|
|
|
//void cpkg_printf(const char *str) {
|
2015-08-10 07:26:06 +00:00
|
|
|
// fprintf(stdout, str);
|
|
|
|
//}
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-08-10 07:41:44 +00:00
|
|
|
// Hi prints hi from Go (via C's stdio)
|
|
|
|
func Hi() {
|
|
|
|
cstr := C.CString("hi from go\n")
|
|
|
|
defer C.free(unsafe.Pointer(cstr))
|
|
|
|
C.cpkg_printf(cstr)
|
|
|
|
}
|
|
|
|
|
2015-08-10 07:26:06 +00:00
|
|
|
// Hello prints a string via C's stdio
|
|
|
|
func Hello(s string) {
|
|
|
|
if s == "" {
|
|
|
|
s = "you"
|
|
|
|
}
|
2015-08-10 07:41:44 +00:00
|
|
|
cstr := C.CString(fmt.Sprintf("hello %s from go\n", s))
|
|
|
|
defer C.free(unsafe.Pointer(cstr))
|
|
|
|
C.cpkg_printf(cstr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf prints a string via C's stdio
|
|
|
|
func Printf(format string, args ...interface{}) {
|
|
|
|
str := fmt.Sprintf(format, args...)
|
|
|
|
cstr := C.CString(str)
|
2015-08-10 07:26:06 +00:00
|
|
|
defer C.free(unsafe.Pointer(cstr))
|
2015-08-10 07:41:44 +00:00
|
|
|
C.cpkg_printf(cstr)
|
2015-08-10 07:26:06 +00:00
|
|
|
}
|