Re: [go-nuts] CGo unexported types

Showing 1-2 of 2 messages
Re: [go-nuts] CGo unexported types Ian Lance Taylor 11/26/12 12:11 PM
On Mon, Nov 26, 2012 at 10:37 AM,  <dpx.in...@gmail.com> wrote:
>
> I asked this question on stackoverflow and still didn't get definite answer,
> so I decided to try here. Text below mostly is copied from the stackoverflow
> question.
>
>
> Suppose there is CGo package with a struct defined like this:
>
> package test
> ...
> type Test struct {
>     Field *C.C_Test
> }
> ...
>
> Now suppose that from somewhere else I get unsafe.Pointer which I know
> points to C_Test C structure.
>
> Do I understand correctly that there is completely no way to create new
> test.Test instance from the unsafe.Pointer value while being in a package
> other than test?

Other than using the unsafe package to push things through pointers, I
believe that is correct.

> Some background: I need a way to create such structure when I use GtkBuilder
> with go-gtk library.
> Its GtkBuilder.GetObject(name) method returns *GObject pointer which in turn
> contains unsafe.Pointer field which I need to turn somehow into, say,
> gtk.GtkEntry struct. gtk.GtkEntry itself contains implicit field of type
> gtk.GtkWidget which has explicit field of type *C.GtkWidget, so I need to
> convert unsafe.Pointer to *C.GtkWidget, but I cannot, as I have described in
> the simple example above.

If that is a reasonable thing to do, then one would hope that the
go-gtk package would provide a way to do it.  I don't know enough
about gtk to know how reasonable it is.

Ian
Re: [go-nuts] CGo unexported types Ian Lance Taylor 11/26/12 3:46 PM
On Mon, Nov 26, 2012 at 2:07 PM,  <dpx.in...@gmail.com> wrote:
>
>> Other than using the unsafe package to push things through pointers, I
>> believe that is correct.
>
> Could you please explain how to do this? I have tried (see stackoverflow
> question and answer) playing with unsafe pointers but couldn't manage to do
> it. I don't know how to beat type system to somehow accept unsafe pointer as
> standard pointer to C struct provided C struct is defined in another
> package.

Untested:

// Assume u is the pointer I want to store.  Presumably it was
obtained from C code in some way.

var t test.Test
p := (*unsafe.Pointer)(unsafe.Pointer(&t.Field))
*p = unsafe.Pointer(u)

Ian

> вторник, 27 ноября 2012 г., 0:11:22 UTC+4 пользователь Ian Lance Taylor
> написал:
> --
>
>