C.GoString()仅返回第一个字符

2024-05-21 08:58:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用c-shared(.so)文件从Python调用Go函数。在我的python代码中,我调用的函数如下:

website = "https://draftss.com"
domain = "draftss.com"
website_ip = "23.xxx.xxx.xxx"

website_tech_finder_lib = cdll.LoadLibrary("website_tech_finder/builds/websiteTechFinder.so")
result_json_string: str = website_tech_finder_lib.FetchAllData(website, domain, website_ip)

在Go端,我正在基于此SO post(out of memory panic while accessing a function from a shared library)将字符串转换为Go字符串:

func FetchAllData(w *C.char, d *C.char, dIP *C.char) *C.char {

    var website string = C.GoString(w)
    var domain string = C.GoString(d)
    var domainIP string = C.GoString(dIP)

    fmt.Println(website)
    fmt.Println(domain)
    fmt.Println(domainIP)
    
    .... // Rest of the code
}

网站域和domainIP只包含我传递的字符串的第一个字符:

fmt.Println(website)  // -> h
fmt.Println(domain)   // -> d
fmt.Println(domainIP) // -> 2

我是新来的,所以我不确定我是不是在做傻事。如何获取传递的完整字符串


Tags: 字符串gostringfindervardomainwebsitetech
1条回答
网友
1楼 · 发布于 2024-05-21 08:58:51

您需要将参数转换为UTF8字节

website = "https://draftss.com".encode('utf-8')
domain = "draftss.com".encode('utf-8')
website_ip = "23.xxx.xxx.xxx".encode('utf-8')

lib = cdll.LoadLibrary("website_tech_finder/builds/websiteTechFinder.so")
result_json_string: str = website_tech_finder_lib.FetchAllData(website, domain, website_ip)

相关问题 更多 >