Go buildmodes prototype: Expose net/http.Server as a library with C bindings
Clone or download



Shared library that exposes Go's net/http.Server with externally-bindable handlers.

This is a silly project for experimenting with Go buildmodes. I gave a talk about this at PyCon 2016.

Status: Tiny subset of the http.HandlerFunc callback gets passed to a C handler callback. Python bindings are working, too.

Getting Started


  • Go 1.5 or newer.
  • C example: make, gcc
  • Python example: cffi, python-cffi
$ git clone https://github.com/shazow/gohttplib/
$ cd gohttplib
$ make examples

Example: C

C example can be linked against a shared object (libgohttp.so generated by go build -buildmode=c-shared) or against a static library archive (libgohttp.a generated by go build -buildmode=c-archive). By default, we link against the shared object because that's what the Python example uses too.

Linked against our shared object:

$ make example-c
$ DYLD_LIBRARY_PATH=build/ ./build/gohttp-c

Note that you'll need to make sure that libgohttp.so is findable at runtime.

Now you can request http://localhost:8000/hello and the C handler in examples/c/main.c will handle it!

Linked against our static library archive:

$ make example-c-static
$ ./build/gohttp-c-static

The static archive gets built into the binary so it's more portable during runtime. Note the size differences:

8.8K  gohttp-c
5.1M  gohttp-c-static
7.5M  libgohttp.a
6.9M  libgohttp.so

Example: Python

The Python example uses python-cffi (you'll need python-cffi installed) to link against the shared object.

$ make example-python
$ cd examples/python
$ python -m gohttp
 * Running on

Or write your own handler using this library:

from gohttp import route, run

def index(w, req):
    w.write("%s %s %s\n" % (req.method, req.host, req.url))
    w.write("Hello, world.\n")

run(host='', port=5000)

References & Credit


This project was made possible thanks to Glider Labs.