Go 語言編寫 CPython 擴充套件 goPy

pythontab發表於2013-04-08

goPy 是一個新的開源專案,實現了用 Go 語言來編寫 CPython 擴充套件。

示例程式碼:

package simple
 
import (
"fmt"
"gopy"
)
 
func example(args *py.Tuple) (py.Object, error) {
fmt.Printf("simple.example: %v\n", args)
py.None.Incref()
return py.None, nil
}
 
func init() {
methods := []py.Method{
{"example", example, "example function"},
}
 
_, err := py.InitModule("simple", methods)
if err != nil {
panic(err)
}
}


編譯方法:

gopy pymodule.go


使用方法:

import simple

simple.example("hello", {123: True})

輸出結果:

simple.example: [hello map[123:true]]


github開源專案地址:https://github.com/qur/gopy


相關文章