您的代码需要2个修复程序:
charchar*C.CStringC.free
以下是您的修复示例:
package main
// FIX: "char response" replaced with "char *response" below.
/*
#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"
"unsafe"
)
type CPoint struct {
Point C.Point
Resp C.Resp
}
func main() {
// FIX: Use CString to allocate a *C.char string.
resp := C.CString("Hello World")
point := CPoint{
Point: C.Point{x: 1, y: 2},
Resp: C.Resp{response: resp},
}
fmt.Println(point)
// Example of converting a C *char to a Go string:
goResp := C.GoString(point.Resp)
fmt.Println(goResp)
// FIX: Release manually allocated string with free(3) when no longer needed.
C.free(unsafe.Pointer(resp))
}