3 ms·
LLGo = Go and C and Python
- xushiwei 2y agoLLGo is a Go compiler based on LLVM in order to better integrate Go with the C ecosystem including Python. Now you can import a Python library in LLGo. For example: package main import ( "github.com/goplus/llgo/c" "github.com/goplus/llgo/py" "github.com/goplus/llgo/py/math" ) func main() { x := math.Sqrt(py.Float(2)) c.Printf(c.Str("sqrt(2) = %f\n"), x.Float64()) } Here, We call py.Float(2) to create a Python number 2, and pass it to Python’s math.sqrt to get x. Then use x.Float64() to convert x to Go's float64 type, and print the value through C printf. Let's look at a slightly more complex example. For example, we use numpy to calculate: package main import ( "github.com/goplus/llgo/c" "github.com/goplus/llgo/py" "github.com/goplus/llgo/py/numpy" ) func main() { a := py.List( py.List(1.0, 2.0, 3.0), py.List(4.0, 5.0, 6.0), py.List(7.0, 8.0, 9.0), ) b := py.List( py.List(9.0, 8.0, 7.0), py.List(6.0, 5.0, 4.0), py.List(3.0, 2.0, 1.0), ) x := numpy.Add(a, b) c.Printf(c.Str("a+b = %s\n"), x.Str().CStr()) } Here we define two 3x3 matrices a and b, add them to get x, and then print the result.