如何将golang字符串添加到cgo中的c结构

How to add a golang string to a c struct in cgo

如何将 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)
}

但是每当我 运行 我得到这个错误 cannot convert r (type string) to type _Ctype_char 我该如何解决?如何将 r 转换为类型 _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;
*/

您的代码需要 2 个修复:

  1. C char 仅表示单个 character/byte - 不是字符串。 C 字符串是 char*.
  2. 您应该使用 C.CString 从 Go 中分配一个 C 字符串,并使用 C.free 释放它。参见:https://pkg.go.dev/cmd/cgo#hdr-Go_references_to_C.

以下是您进行这些修复的示例:

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;

void fill(const char *name, Resp *r) {
   printf("name: %s\n", name);
   printf("Original value: %s\n", r->response);
   r->response = "testing";
}
*/
import "C"
import (
    "fmt"
    "unsafe"
)

// NOTE: You cannot pass this struct to C.
// Types passed to C must be defined in C (eg, like "Point" above).
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 calling a C function and converting a C *char string to a Go string:
    cname := C.CString("foo")
    defer C.free(unsafe.Pointer(cname))
    r := &C.Resp{
        response: resp, // Use allocated C string.
    }
    C.fill(cname, r)
    goResp := C.GoString(r.response)
    fmt.Println(goResp)

    // FIX: Release manually allocated string with free(3) when no longer needed.
    C.free(unsafe.Pointer(resp))
}