我将使用cgo将一个c库包装为go库,以供项目使用。我阅读了文档,使用cgo时似乎有很多规则。我不知道这是否合法。

LibCtx和Client都是C中的结构。这是将C结构放入golang结构的合法方法吗?

//DBClientLib.go

type DBClient struct {
    Libctx C.LibCtx
    LibClient C.Client
}

func (client DBClient) GetEntry(key string) interface{} {

    //...
}

最佳答案

是的,这完全合法。看看这个简短的例子:

package main

/*
typedef struct Point {
    int x , y;
} Point;
*/
import "C"
import "fmt"

type CPoint struct {
    Point C.Point
}

func main() {
    point := CPoint{Point: C.Point{x: 1, y: 2}}
    fmt.Printf("%+v", point)
}

输出值
{Point:{x:1 y:2}}

07-26 09:29
查看更多