簡體   English   中英

CGO中帶有C結構的golang結構

[英]golang struct with C struct in CGO

我將使用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)
}

OUTPUT

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM