Go 语言编写 CPython 扩展 goPy

goPy 是一个新的开源项目,实现了用 Go 语言来编写 CPython 扩展。

示例代码:

01 package simple
02  
03 import (
04     "fmt"
05     "gopy"
06 )
07  
08 func example(args *py.Tuple) (py.Object, error) {
09     fmt.Printf("simple.example: %v\n", args)
10     py.None.Incref()
11     return py.None, nil
12 }
13  
14 func init() {
15     methods := []py.Method{
16         {"example", example, "example function"},
17     }
18  
19     _, err := py.InitModule("simple", methods)
20     if err != nil {
21         panic(err)
22     }
23 }

编译方法:

1 > gopy pymodule.go

使用方法:

1 import simple
2  
3 simple.example("hello", {123: True})

输出结果:

1 simple.example: [hello map[123:true]]
原文地址:https://www.cnblogs.com/shihao/p/3009352.html