Created
September 30, 2025 07:05
-
-
Save bigwhite/b8ac87d5a1efed44838e6e123038c256 to your computer and use it in GitHub Desktop.
main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ctypes | |
| import os | |
| # 1. 加载 Go 编译的动态库 | |
| # 注意:路径可能需要根据你的操作系统调整 | |
| lib = ctypes.CDLL(os.path.join(os.getcwd(), "libmymath.so")) | |
| # 2. 定义函数的参数和返回类型 (类型映射) | |
| lib.Add.argtypes = [ctypes.c_int, ctypes.c_int] | |
| lib.Add.restype = ctypes.c_int | |
| lib.Subtract.argtypes = [ctypes.c_int, ctypes.c_int] | |
| lib.Subtract.restype = ctypes.c_int | |
| # 3. 像调用普通 Python 函数一样调用它! | |
| sum_result = lib.Add(50, 25) | |
| print(f"50 + 25 = {sum_result}") | |
| diff_result = lib.Subtract(50, 25) | |
| print(f"50 - 25 = {diff_result}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment