Setuptools Golang Extension
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
- breaking The project is officially deprecated and archived. The maintainer states that 'multiple go shared objects in a single process is not supported' and 'it likely broke in go 1.21 and there is no intention to fix it'. This means extensions built with setuptools-golang may not work with Go versions 1.21 and newer.
- gotcha When building extensions that involve C code (e.g., CGo), related C files might not be included in the distribution by default, leading to 'undefined reference' errors during linking.
- gotcha Incorrect or non-existent external Go import paths can lead to 'fatal: could not read Username for 'https://github.com'' or 'package github.com/a/b/c: ... exists but .../.git does not' errors.
- gotcha Duplicate symbol errors (e.g., `duplicate symbol _XXX`) can occur if global variables defined in C code are not correctly marked for Go's CGo interaction.
- gotcha Repeated builds can be slow due to Go's default GOPATH management.
Install
-
pip install setuptools-golang
Imports
- Extension
from setuptools import Extension
Quickstart
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))