mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
ch2-07: 实现例子
This commit is contained in:
parent
6c1741df63
commit
c8b50d63e0
11
examples/ch2-07/hello-py/Makefile
Normal file
11
examples/ch2-07/hello-py/Makefile
Normal file
@ -0,0 +1,11 @@
|
||||
default:
|
||||
go build -buildmode=c-shared -o gopkg.so main.go
|
||||
python3 -c 'import gopkg; print(gopkg.system("time"))'
|
||||
|
||||
clean:
|
||||
-rm *.so
|
||||
|
||||
# PKG_CONFIG=python3-config go build -buildmode=c-shared -o say-hello.so main.go
|
||||
# python3-config --ldflags
|
||||
# python3-config --include
|
||||
|
51
examples/ch2-07/hello-py/main.go
Normal file
51
examples/ch2-07/hello-py/main.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
// macOS:
|
||||
// python3-config --cflags
|
||||
// python3-config --ldflags
|
||||
#cgo darwin CFLAGS: -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -g
|
||||
#cgo darwin LDFLAGS: -L/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin -lpython3.6m -ldl -framework CoreFoundation
|
||||
|
||||
// linux
|
||||
#cgo linux pkg-config: python3
|
||||
|
||||
// windows
|
||||
// should generate libpython3.a from python3.lib
|
||||
|
||||
#define Py_LIMITED_API
|
||||
#include <Python.h>
|
||||
|
||||
static PyObject *
|
||||
spam_system(PyObject *self, PyObject *args) {
|
||||
const char *command;
|
||||
if (!PyArg_ParseTuple(args, "s", &command)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int status = system(command);
|
||||
return PyLong_FromLong(status);
|
||||
}
|
||||
|
||||
static PyMethodDef modMethods[] = {
|
||||
{"system", spam_system, METH_VARARGS, "Execute a shell command."},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
void* PyInit_gopkg(void) {
|
||||
static struct PyModuleDef module = {
|
||||
PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, modMethods,
|
||||
};
|
||||
return (void*)PyModule_Create(&module);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {}
|
||||
|
||||
// export SayHello
|
||||
func SayHello(name *C.char) {
|
||||
fmt.Printf("hello %s!\n", C.GoString(name))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user