简体   繁体   中英

cgo result has go pointer

I am writing some go code that exports a function like that:

package main
import "C"

//export returnString
func returnString() string {
    //
    gostring := "hello world"
    return gostring
}
func main() {}

I build the .so and the header file by using go build -buildmode=c-shared , but when I call returnString() in my C code, I get panic: runtime error: cgo result has Go pointer

Is there a way to to this in go 1.9?

You need to convert your go string to *C.char . C.Cstring is utility function for that.

package main

import "C"

//export returnString
func returnString() *C.char {
    gostring := "hello world"
    return C.CString(gostring)
}

func main() {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM