tech:inclure_des_appels_a_go_lang_dans_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
| tech:inclure_des_appels_a_go_lang_dans_python [2026/04/18 16:14] – créée Jean-Baptiste | tech:inclure_des_appels_a_go_lang_dans_python [2026/04/18 17:02] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | |||
| + | # Inclure des appels à Go lang dans Python | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | |||
| + | Voir aussi : | ||
| + | * [[Executer du code Python avec Go lang]] | ||
| + | |||
| + | |||
| + | ## Tuto 1 | ||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | |||
| + | |||
| + | Linux and Mac | ||
| + | |||
| + | ~~~bash | ||
| + | go build -buildmode=c-shared -o add.so add.go | ||
| + | ~~~ | ||
| + | |||
| + | windows | ||
| + | ~~~bash | ||
| + | go build -buildmode=c-shared -o add.dll add.go | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | go build -ldflags "-s -w" -buildmode=c-shared -o add.so add.go | ||
| + | ~~~ | ||
| + | |||
| + | `-s` indicates compression | ||
| + | `-w` indicates removing debugging | ||
| + | |||
| + | ~~~python | ||
| + | # Test calling code written in the GO language from Python | ||
| + | # First, the GO code needs to be compiled into a dynamic link library | ||
| + | |||
| + | from ctypes import cdll | ||
| + | |||
| + | lib = cdll.LoadLibrary(" | ||
| + | |||
| + | # Call the Add function in the GO language | ||
| + | result = lib.Add(100, | ||
| + | |||
| + | print(" | ||
| + | |||
| + | # Call the PrintDll function in the GO language | ||
| + | lib.PrintDll() | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Tuto 2 | ||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | ### Exemple 1 | ||
| + | |||
| + | `library.go` | ||
| + | ~~~go | ||
| + | package main | ||
| + | |||
| + | import ( | ||
| + | " | ||
| + | " | ||
| + | ) | ||
| + | |||
| + | //export helloWorld | ||
| + | func helloWorld(){ | ||
| + | | ||
| + | } | ||
| + | |||
| + | func main(){ | ||
| + | |||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | Compilation de la lib | ||
| + | ~~~bash | ||
| + | go build -buildmode=c-shared -o library.so library.go | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Execution depuis Python | ||
| + | ~~~python | ||
| + | import ctypes | ||
| + | library = ctypes.cdll.LoadLibrary(' | ||
| + | hello_world = library.helloWorld | ||
| + | hello_world() | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Exemple 2 | ||
| + | |||
| + | `library.go` | ||
| + | ~~~go | ||
| + | //export hello | ||
| + | func hello(namePtr *C.char){ | ||
| + | name := C.GoString(namePtr) | ||
| + | | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | `app.py` | ||
| + | ~~~python | ||
| + | hello = library.hello | ||
| + | hello.argtypes = [ctypes.c_char_p] | ||
| + | hello(" | ||
| + | ~~~ | ||
| + | |||
| + | `library.go` | ||
| + | ~~~go | ||
| + | //export farewell | ||
| + | func farewell() *C.char{ | ||
| + | | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | FIXME | ||
| + | |||
