Important
Currently supports python-3.12 only.
The package provides a higher level for interacting with Python-3 C-API without directly using the PyObject.
Main goals:
- Provide generic a
Objectthat interact with Golang types. - Automatically marshal and unmarshal Golang types to Python types.
Go >= 1.24Python = 3.12
go get go.nhat.io/python/v3package main
import (
"fmt"
python3 "go.nhat.io/python/v3"
)
funcmain() {
sys:=python3.MustImportModule("sys")
version:=sys.GetAttr("version_info")
pyMajor:=version.GetAttr("major")
deferpyMajor.DecRef()
pyMinor:=version.GetAttr("minor")
deferpyMinor.DecRef()
pyReleaseLevel:=version.GetAttr("releaselevel")
deferpyReleaseLevel.DecRef()
major:=python3.AsInt(pyMajor)
minor:=python3.AsInt(pyMinor)
releaseLevel:=python3.AsString(pyReleaseLevel)
fmt.Println("major:", major)
fmt.Println("minor:", minor)
fmt.Println("release level:", releaseLevel)
// Output:// major: 3// minor: 11// release level: final
}package main
import (
"fmt"
python3 "go.nhat.io/python/v3"
)
funcmain() { //nolint: govetmath:=python3.MustImportModule("math")
pyResult:=math.CallMethodArgs("sqrt", 4)
deferpyResult.DecRef()
result:=python3.AsFloat64(pyResult)
fmt.Printf("sqrt(4) = %.2f\n", result)
// Output:// sqrt(4) = 2.00
}If this project help you reduce time to develop, you can give me a cup of coffee :)
or scan this

