2019-02-13 09:23



I'm trying to use cairo bindings (Go package), which defines a wrapper structure with a C type in it, with a C function, but can't make it work.

The cairo package defines a Context:

package cairo
type Context struct {
    Ptr *C.cairo_t

Then, from the main package, I want to use its pointer:

package main
cr := cairo.Create(surf.Surface)
layout := C.pango_cairo_create_layout(cr.Ptr)

The C.pango_cairo_create_layout accepts *C.cairo_t, which is what cr.Ptr should be. However, the go compiler doesn't agree:

./main.go:114:157: cannot use cr.Ptr (type *cairo._Ctype_struct__cairo) as type *_Ctype_struct__cairo in argument to func literal

As can be seen, cr.Ptr is of type *cairo.C.cairo_t and not *C.cairo_t, that is, the package namespace is a part of the type.

I've found, which talks about a workaround using the combination of the reflect package and unsafe.Pointer. However, I can't make it work.

How do I correctly "bend" this so it compiles and works?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanjia7607 duanjia7607 2年前

    For those interested, I've figured it out.

    package main
    cr := cairo.Create(surf.Surface)
    ptr := (*C.cairo_t)(unsafe.Pointer(reflect.ValueOf(cr.Ptr).Pointer()))
    layout := C.pango_cairo_create_layout(ptr)

    Maybe it can be done more simply, but this works.

    点赞 评论 复制链接分享