Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:45:15+00:00 2026-06-15T09:45:15+00:00

Suppose there is CGo package with a struct defined like this: package test …

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:45:17+00:00Added an answer on June 15, 2026 at 9:45 am

    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:

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

    So, all what was needed is double cast to unsafe.Pointer and then to *unsafe.Pointer. I came up with the following code to simplify the assign process:

    func Assign(to unsafe.Pointer, from unsafe.Pointer) {
        fptr := (*unsafe.Pointer)(from)
        tptr := (*unsafe.Pointer)(to)
        *tptr = *fptr
    }
    
    func Init(builder *gtk.GtkBuilder) {
        messageNameEntryWidget := gtk.GtkWidget{}
        Assign(unsafe.Pointer(&messageNameEntryWidget.Widget),
               unsafe.Pointer(&builder.GetObject("messageNameEntry").Object))
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose there is a method like this (C#): public static int Add(int x, int
Suppose there was a SQL table like this: Label Name | Last name |
Suppose there is a data field education in my table profile, now I want
Suppose there's a system like codepad.org - a black box that can accept C++
Suppose there is a structure like this: {'key1' : { 'key2' : { ....
Suppose there is a table like this: f1 f2 ---------- 1 3 4 8
suppose there is a script doing something like this: # module writer.py import sys
Suppose there is a script sth.py When i logged with 'root' account. type :
Suppose there is a situation like : statement 1; statement 2; statement 3; statement
Suppose there is a System A which gives some output. This output is used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.