mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 20:52:22 +00:00
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
// Copyright © 2017 ChaiShushan <chaishushan{AT}gmail.com>.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
package main
|
|
|
|
/*
|
|
// macOS:
|
|
#cgo darwin pkg-config: python3
|
|
|
|
// linux
|
|
#cgo linux pkg-config: python3
|
|
|
|
// windows
|
|
// should generate libpython3.a from python3.lib
|
|
|
|
#define Py_LIMITED_API
|
|
#include <Python.h>
|
|
|
|
extern PyObject* PyInit_gopkg();
|
|
extern PyObject* Py_gopkg_sum(PyObject *, PyObject *);
|
|
|
|
static int cgo_PyArg_ParseTuple_ii(PyObject *arg, int *a, int *b) {
|
|
return PyArg_ParseTuple(arg, "ii", a, b);
|
|
}
|
|
|
|
static PyObject* cgo_PyInit_gopkg(void) {
|
|
static PyMethodDef methods[] = {
|
|
{"sum", Py_gopkg_sum, METH_VARARGS, "Add two numbers."},
|
|
{NULL, NULL, 0, NULL},
|
|
};
|
|
static struct PyModuleDef module = {
|
|
PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, methods,
|
|
};
|
|
return PyModule_Create(&module);
|
|
}
|
|
*/
|
|
import "C"
|
|
|
|
func main() {}
|
|
|
|
//export PyInit_gopkg
|
|
func PyInit_gopkg() *C.PyObject {
|
|
return C.cgo_PyInit_gopkg()
|
|
}
|
|
|
|
//export Py_gopkg_sum
|
|
func Py_gopkg_sum(self, args *C.PyObject) *C.PyObject {
|
|
var a, b C.int
|
|
if C.cgo_PyArg_ParseTuple_ii(args, &a, &b) == 0 {
|
|
return nil
|
|
}
|
|
return C.PyLong_FromLong(C.long(a + b))
|
|
}
|