bpl
bpl

Reputation: 51

golang struct with C struct in CGO

I will use cgo to wrap one c library as go library for the project usage. I read the document, it seems there are lots of rules while using 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?

//DBClientLib.go

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

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

    //...
}

Upvotes: 5

Views: 11568

Answers (1)

Seaskyways
Seaskyways

Reputation: 3795

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

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

Upvotes: 7

Related Questions