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?
Attempting to use something like &test.Test{ptr}, where ptr is unsafe.Pointer value, fails for obvious reasons with message cannot use ptr (type unsafe.Pointer) as type *test._Ctype_C_Test in field value, and type conversion to *test._Ctype_C_Test does not work as well because this type is not exported.
And I think that making my other module to use CGo and redefining the same C structure in it will not work either since that package will have something like client._Ctype_C_Test but test.Test requires test._Ctype_C_Test, and they are different from the point of view of the typechecker.
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.
Update: here is the code I’m trying to force to work: https://gist.github.com/4141343
I asked a question on golang-nuts and was given an example on how to do what I want. It seems to work now. Here is the code from Ian’s answer:
So, all what was needed is double cast to
unsafe.Pointerand then to*unsafe.Pointer. I came up with the following code to simplify the assign process:Then, however, I decided to abstain from using GTK, since it is painful to do tricks like this) but this is not related to the question.