Skip to content

Instantly share code, notes, and snippets.

@astagi
Last active September 17, 2018 09:07
Show Gist options
  • Save astagi/75b9094ed882507bb96e4f55dd881e2c to your computer and use it in GitHub Desktop.
Save astagi/75b9094ed882507bb96e4f55dd881e2c to your computer and use it in GitHub Desktop.
Extending Python with Go - Part 1
#include <Python.h>
static PyObject *sum(PyObject *self, PyObject *args) {
const long a, b;
if (!PyArg_ParseTuple(args, "LL", &a, &b))
return NULL;
return PyLong_FromLong(a + b);
}
static PyMethodDef MathMethods[] = {
{"sum", sum, METH_VARARGS, "Add two numbers."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef newmathmodule = {
PyModuleDef_HEAD_INIT, "newmath", NULL, -1, MathMethods
};
PyMODINIT_FUNC PyInit_newmath(void) {
return PyModule_Create(&newmathmodule);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment