-
Notifications
You must be signed in to change notification settings - Fork 128
/
go_python.slide
115 lines (65 loc) · 1.84 KB
/
go_python.slide
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Calling Go from Python
Pavel Tišnovský
https://github.com/RedHatOfficial/GoCourse
## Go + Python
.image ./common/golang.png
.image ./common/python.png
## Two different worlds
* Python has support for FFI, cffi, ctypes etc.
* It is possible to compile Go library to .so/.dll
- integration seems easy
* But
- in fact the integration is based on common ground: C
- different data types
- totally different data structures
- GC on both sides! (different ones)
* Let's try more convenient approaches first
## Step #1: how to integrate Go with C
* Go library with exported function
* Compilation into shared library (DLL, so)
* Loading library into C
## Go library
.code go_python/example1.go
## Compile and build a library
```
go build -buildmode=c-shared example1.go
```
## Loading Go library from C (1/2)
.code go_python/example1A.c
## Loading Go library from C (2/2)
.code go_python/example1B.c
* Compile and build
```
gcc -ansi example1.c -ldl
```
## Step 2: now use Python instead of C
## System-wide library (or LD_LIBRARY_PATH)
.code go_python/example1A.py
## Local library
.code go_python/example1B.py
## `main` and `init` functions possible
.code go_python/example2.go
## Go function with parameters and return value
.code go_python/example3.go
## Call from Python
.code go_python/example3A.py
## Common problems
.code go_python/example3B.py
.code go_python/example3C.py
## System-independent data types
.code go_python/example4.go
## Python side
.code go_python/example4A.py
## Max vals
.code go_python/example4B.py
.code go_python/example4C.py
## Passing strings to Go
.code go_python/example5.go
## Strings on Python side
.code go_python/example5.py
## Type `*C.char`
.code go_python/example6.go
## Strings on Python side
.code go_python/example6A.py
.code go_python/example6B.py
.code go_python/example6C.py