You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-10Lines changed: 11 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,9 +46,9 @@ xcode-select --install
46
46
```
47
47
</details>
48
48
49
-
## Quick Start
49
+
## Python calling Go
50
50
51
-
### 1. Write Go Code
51
+
### 1. Export Go Functions/Types
52
52
53
53
Create a `lib.go` file:
54
54
@@ -130,25 +130,22 @@ Run the python script:
130
130
python main.py
131
131
```
132
132
133
-
## Go Call Python
133
+
## Go calling Python
134
134
135
-
### 1. Define Python Functions/Classes
135
+
### 1. Export Python Functions/Classes
136
136
137
137
```python
138
138
# main.py
139
139
import gopylink
140
140
141
-
142
141
@gopylink.export
143
142
defgreet(name: str) -> str:
144
143
returnf"Hi, {name}!"
145
144
146
-
147
145
@gopylink.export
148
146
defmultiply(a, b):
149
147
return a * b
150
148
151
-
152
149
@gopylink.export
153
150
classCounter:
154
151
def__init__(self, initial=0):
@@ -158,9 +155,9 @@ class Counter:
158
155
self.value += n
159
156
returnself.value
160
157
161
-
162
-
lib = gopylink.load_go_lib("go.lib")
163
-
lib.func_call("Start") # Start the Go logic
158
+
if__name__=="__main__":
159
+
lib = gopylink.load_go_lib("go.lib")
160
+
lib.func_call("Start") # Start the Go logic
164
161
```
165
162
166
163
### 2. Call Python from Go
@@ -209,6 +206,8 @@ go build -buildmode=c-shared -o go.lib .
209
206
python main.py
210
207
```
211
208
209
+
Note that when calling Python from Go, the entry point remains the Python script. The Go shared library is loaded into the Python process, which then starts the Go logic.
210
+
212
211
## API Reference
213
212
214
213
### Python API
@@ -232,6 +231,8 @@ python main.py
232
231
|`handle.MethodCall(name, args...)`| Call method on Python instance |
233
232
|`handle.Close()`| Release Python instance |
234
233
234
+
See [Go API Reference](https://pkg.go.dev/github.com/ray4go/gopylink#pkg-index) for more details.
0 commit comments