CGO结果有去指针 [英] cgo result has go pointer

查看:70
本文介绍了CGO结果有去指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

在1.9版中,有没有办法做到这一点?

Is there a way to to this in go 1.9?

推荐答案

您需要将go字符串转换为 * C.char . C.Cstring 是实用程序功能.

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

这篇关于CGO结果有去指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆