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

huangapple go评论90阅读模式
英文:

C.GoString() only returning first character

问题

我正在尝试使用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端,我根据这个Stack Overflow帖子(https://stackoverflow.com/questions/36482970/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)
    
    .... // 其余代码
}

网站域名和域名IP只有我传递的字符串的第一个字符:

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

我对Go还不太熟悉,所以不确定我是否在这里做了一些愚蠢的事情。如何获取我传递的完整字符串?

英文:

I'm trying to call a Go function from Python using c-shared (.so) file. In my python code I'm calling the function like this:

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)

On Go side I'm converting the strings to Go strings based on this SO post (https://stackoverflow.com/questions/36482970/out-of-memory-panic-while-accessing-a-function-from-a-shared-library):

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
}

The website domain and domainIP just have the first characters of the strings that I passed:

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

I'm a bit new to Go, so I'm not sure if I'm doing something stupid here. How do I get the full string that I passed?

答案1

得分: 3

你需要将参数转换为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)
英文:

You need to convert the parameters as UTF8 bytes.

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)

huangapple
  • 本文由 发表于 2021年10月4日 17:58:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/69434070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定