This repository was archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (63 loc) · 1.59 KB
/
main.go
File metadata and controls
76 lines (63 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"log"
"github.com/sbinet/go-python"
)
func init() {
err := python.Initialize()
if err != nil {
log.Panic(err.Error())
}
}
func main() {
module := python.PyImport_ImportModule("values")
if module == nil {
log.Fatal("could not import 'values'")
}
name := module.GetAttrString("__name__")
if name == nil {
log.Fatal("could not getattr '__name__'")
}
defer name.DecRef()
fmt.Printf("values.__name__: %q\n", python.PyString_AsString(name))
sval := module.GetAttrString("sval")
if sval == nil {
log.Fatal("could not getattr 'sval'")
}
defer sval.DecRef()
fmt.Printf("values.sval: %q\n", python.PyString_AsString(sval))
pyival := module.GetAttrString("ival")
if pyival == nil {
log.Fatal("could not getattr 'ival'")
}
defer pyival.DecRef()
ival := python.PyInt_AsLong(pyival)
fmt.Printf("values.ival: %d\n", ival)
myfunc := module.GetAttrString("myfunc")
if myfunc == nil {
log.Fatal("could not getattr 'myfunc'")
}
defer myfunc.DecRef()
o1 := myfunc.CallFunction()
if o1 == nil {
log.Fatal("could not call 'values.myfunc()'")
}
fmt.Printf("%s\n", python.PyString_AsString(o1))
o1.DecRef()
// modify 'test.ival' and 'test.sval'
{
pyival := python.PyInt_FromLong(ival + 1000)
module.SetAttrString("ival", pyival)
pyival.DecRef()
pysval := python.PyString_FromString(python.PyString_AsString(sval) + " is the answer")
module.SetAttrString("sval", pysval)
pysval.DecRef()
}
o2 := myfunc.CallFunction()
if o2 == nil {
log.Fatal("could not call 'values.myfunc()'")
}
fmt.Printf("%s\n", python.PyString_AsString(o2))
o2.DecRef()
}