Search code examples
pythongo

Mixing Python and Go


I have been working on a library in Python and I would like to do some performance improvement.

Is it possible to write some code in Python and some code in Go, and pass data between them? And if it's possible, are there any examples on how to do this?

Like such:

# Python
def python_foo():
    data = {'foo': 'val', 'bar': [1, 2, 3]}
    go_process(json.dumps(data))


def python_got_data_from_go(data):
    # deal with data from Go


# Go
func go_process(json string) {
    // do some processing
    python_got_data_from_go(someData)
}

Solution

  • You need a glue between them, for example C programming language or communication over network. Most painful solution if you mix https://docs.python.org/2/extending/extending.html with http://golang.org/cmd/cgo/ and good programming skills in C.

    You might create server in python http://pymotw.com/2/socket/tcp.html and in go https://coderwall.com/p/wohavg and communicate between them.

    Edit: see Writing a Python extension in Go (Golang) and Calling Python function from Go and getting the function return value