简体   繁体   English

CGO中带有C结构的golang结构

[英]golang struct with C struct in CGO

I will use cgo to wrap one c library as go library for the project usage. 我将使用cgo将一个c库包装为go库,以供项目使用。 I read the document, it seems there are lots of rules while using cgo. 我阅读了文档,使用cgo时似乎有很多规则。 I don't know whether this is legal or not. 我不知道这是否合法。

Both LibCtx and Client is a struct in C. Is this a legal way to put C struct into a golang struct? LibCtx和Client都是C中的结构。这是将C结构放入golang结构的合法方法吗?

//DBClientLib.go

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

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

    //...
}

Yes, this is totally legal. 是的,这完全合法。 Check out this short example: 看看这个简短的例子:

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

OUTPUT OUTPUT

{Point:{x:1 y:2}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM