python调用go

shared.go

package main

import "C"

//export add
func add(a, b int) int {
    return a + b
}

//export append_str
func append_str(a, b *C.char) *C.char {
    merge := C.GoString(a) + C.GoString(b)
    return C.CString(merge)
}

//go build -buildmode=c-shared -o demo.so shared.go
func main() {
}

python

# https://www.cxyzjd.com/article/weixin_37989267/108869265
import ctypes

lib = ctypes.cdll.LoadLibrary('/home/xxx/projects/golang/demo/demo.so')

result = lib.add(2020, 1)
print(result)

# 调用go模块
go_append_str = lib.append_str

# 显式声明参数和返回的期望类型
go_append_str.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
go_append_str.restype = ctypes.c_char_p
result_bytes = go_append_str("测试".encode("utf-8"),'数据'.encode("utf-8"))
result = result_bytes.decode("utf-8")
print(result)

Original: https://www.cnblogs.com/ziyouchutuwenwu/p/15308243.html
Author: 自由出土文物
Title: python调用go

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15844/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部