Home > Back-end >  C.GoString() only returning first character
C.GoString() only returning first character

Time:10-04

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 (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?

CodePudding user response:

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)
  • Related