如何将 golang 字符串添加到我在 cgo 中制作的 c 结构中。代码如下:
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
typedef struct Point {
int x, y;
} Point;
typedef struct Resp {
char response;
} Resp;
*/
import "C"
import (
"fmt"
"io/ioutil"
"unsafe"
)
type CPoint struct {
Point C.Point
Resp C.Resp
}
func main() {
var r string = "Hello World"
resp := unsafe.Pointer(C.char(r))
point := CPoint{
Point: C.Point{x: 1, y: 2},
Resp: C.Resp{response: resp},
}
fmt.Println(point)
}
但是每当我运行这个,我得到这个错误如何解决这个问题?如何通过 r 键入 _Ctype_char?cannot convert r (type string) to type _Ctype_char
另外,如何在 c 结构“Resp”中获取值?
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
typedef struct Point {
int x, y;
} Point;
typedef struct Resp {
char response; // <-- How can I get this value in my go code?
} Resp;
*/
森栏
相关分类