Setuptools Golang Extension

2.9.0 · deprecated · verified Sat Apr 11

setuptools-golang is a setuptools extension designed for building CPython extensions written in Go. It enables developers to integrate Go-based modules directly into Python packages. The library is currently at version 2.9.0, but its GitHub repository was archived in January 2025, indicating it is no longer actively maintained. The primary maintainer has stated that multiple Go shared objects in a single process are not supported by Go 1.21+ and there is no intention to fix this, severely limiting its utility with modern Go versions.

Warnings

Install

Imports

Quickstart

A `setup.py` demonstrating how to define a Go extension using `setuptools-golang`. The `build_golang` dictionary specifies the root Go import path, and `Extension` points to the Go source file(s). This example creates a simple Go function `Sum` that adds two integers, exposed to Python.

import os
from setuptools import setup, Extension

# Create a dummy Go source file for the example
with open('example.go', 'w') as f:
    f.write("""
package main

import "C"

//export Sum
func Sum(a, b int) int {
	return a + b
}

func main() {}
""")

setup(
    name='my_go_extension',
    version='0.1.0',
    description='A Python extension in Go',
    setup_requires=['setuptools-golang'],
    build_golang={'root': 'github.com/user/project'},
    ext_modules=[
        Extension(
            'my_go_extension.example',
            ['example.go'],
        ),
    ],
)

# To demonstrate usage, you would typically run:
# python setup.py build_ext --inplace
# Then in Python:
# import my_go_extension.example
# print(my_go_extension.example.Sum(1, 2))

view raw JSON →