Skip to content

Commit e3d712d

Browse files
author
g.ph
committed
update
1 parent 2dfd25a commit e3d712d

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
### go python3 bind
3+
* ```python version:3.10.5```
34

45

56
### support list

_examples/README.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Table of Contents
2+
* [base](base)
3+
* [go module register to py](gofunc)

_examples/base/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
"github.com/aadog/py3-go"
6+
"github.com/aadog/py3-go/cpy3"
7+
"os"
8+
)
9+
10+
//go:embed main.py
11+
var PyMain string
12+
13+
func main() {
14+
cpy3.Py_SetProgramName(os.Args[0])
15+
cpy3.Py_SetPythonHome("./")
16+
py3.Initialize()
17+
cpy3.PyRun_SimpleString(PyMain)
18+
py3.Finalize()
19+
}

_examples/base/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
def main():
3+
print("hello python3")
4+
5+
6+
if __name__ == '__main__':
7+
main()

_examples/gofunc/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
"github.com/aadog/py3-go"
6+
"github.com/aadog/py3-go/cpy3"
7+
"os"
8+
)
9+
10+
//go:embed main.py
11+
var PyMain string
12+
13+
func main() {
14+
cpy3.Py_SetProgramName(os.Args[0])
15+
cpy3.Py_SetPythonHome("./")
16+
py3.PyImport_AppendInittab("gofunc", func() *py3.PyObject {
17+
m := py3.CreateModule("gofunc", "gofunc")
18+
m.AddFunction("add", func(a int, b int) int {
19+
return a + b
20+
})
21+
m.AddFunction("py", func(a *py3.PyObject) *py3.PyObject {
22+
return a
23+
})
24+
return m.AsObj()
25+
})
26+
py3.Initialize()
27+
cpy3.PyRun_SimpleString(PyMain)
28+
py3.Finalize()
29+
}

_examples/gofunc/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
gofunc=__import__("gofunc")
2+
3+
4+
def main():
5+
print("add:{}".format(gofunc.Call("gofunc.add",1,2)))
6+
print("py:{}".format(gofunc.Call("gofunc.py","py string")))
7+
8+
9+
if __name__ == '__main__':
10+
main()

0 commit comments

Comments
 (0)