cgo result has go pointer

ⅰ亾dé卋堺 提交于 2019-12-11 16:20:24

问题


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?


回答1:


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() {}


来源:https://stackoverflow.com/questions/48686763/cgo-result-has-go-pointer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!