我正在编写一些导出这样的函数的代码:
package main
import "C"
//export returnString
func returnString() string {
//
gostring := "hello world"
return gostring
}
func main() {}
我使用 go build -buildmode=c-shared
构建了 .so
和头文件,但是当我调用 returnString()
在我的 C 代码中,我得到 panic: runtime error: cgo result has Go pointer
在 go 1.9 中有办法做到这一点吗?
最佳答案
您需要将您的 go 字符串转换为 *C.char
。 C.Cstring
是它的实用函数。
package main
import "C"
//export returnString
func returnString() *C.char {
gostring := "hello world"
return C.CString(gostring)
}
func main() {}
关于cgo 结果有 go 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686763/