dongzhuo1733
2018-02-08 13:34
浏览 76
已采纳

cgo结果有指针

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?

图片转代码服务由CSDN问答提供 功能建议

我正在编写一些go代码,该代码可以导出如下函数:

  包main 
import“ C” 
 
 // export returnString 
func returnString()string {
 // 
 gostring:=“ hello world” 
 return gostring 
} 
func main()  {} 
   
 
 

我使用go build -buildmode = c-shared来构建.so和头文件,但是当我在C代码中调用returnString()时 ,我收到“紧急情况:运行时错误:cgo结果具有Go指针”

在1.9中是否可以解决此问题?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtan6336 2018-02-08 13:47
    已采纳

    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() {}
    
    点赞 评论

相关推荐 更多相似问题