FAQ
package main

/*
typedef union _B{
         int b;
         int c;
}B;
typedef struct _A{
         int a;
         B b;
}A;

*/
import "C"

func main() {
         var a C.A
         a.a = 0
         a.b.b = 0
}


a.b.b will not be found in struct A while building.
It works OK in c compiler like gcc.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Carlos Castillo at Sep 15, 2013 at 12:40 pm
    CGO converts the go-code you wrote (using the C import), into valid go
    function calls, pointers, structs, and variables. It also provides C-code
    that converts between go's runtime conventions and C's. For every value,
    pointer, and struct, an equivalent go type is made, so the memory is used
    directly. The important thing to take out of this is that the go code it
    produces is in no way special compared to any you'd write.

    Go doesn't have unions, and go structs can't have fields that alias other
    fields, so C unions can not be represented by an "equivalent" type. They
    are instead treated as a block of memory in the form of a byte array.

    If you run "go tool cgo main.go" you will see in _obj/_cgo_gotypes.go that
    C.B is defined as "type _Ctype_union__B [4]byte".

    You must use a unsafe.Pointer cast to read/write the appropriate type. eg:

    ptr := (*int32)(unsafe.Pointer(&a.b[0]))
    *ptr = 42

    On Sunday, September 15, 2013 12:11:23 AM UTC-7, Allan Xu wrote:

    package main

    /*
    typedef union _B{
    int b;
    int c;
    }B;
    typedef struct _A{
    int a;
    B b;
    }A;

    */
    import "C"

    func main() {
    var a C.A
    a.a = 0
    a.b.b = 0
    }


    a.b.b will not be found in struct A while building.
    It works OK in c compiler like gcc.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Allan Xu at Sep 16, 2013 at 2:45 am
    thank you Carlos:)
    On Sunday, September 15, 2013 8:40:35 PM UTC+8, Carlos Castillo wrote:

    CGO converts the go-code you wrote (using the C import), into valid go
    function calls, pointers, structs, and variables. It also provides C-code
    that converts between go's runtime conventions and C's. For every value,
    pointer, and struct, an equivalent go type is made, so the memory is used
    directly. The important thing to take out of this is that the go code it
    produces is in no way special compared to any you'd write.

    Go doesn't have unions, and go structs can't have fields that alias other
    fields, so C unions can not be represented by an "equivalent" type. They
    are instead treated as a block of memory in the form of a byte array.

    If you run "go tool cgo main.go" you will see in _obj/_cgo_gotypes.go that
    C.B is defined as "type _Ctype_union__B [4]byte".

    You must use a unsafe.Pointer cast to read/write the appropriate type. eg:

    ptr := (*int32)(unsafe.Pointer(&a.b[0]))
    *ptr = 42

    On Sunday, September 15, 2013 12:11:23 AM UTC-7, Allan Xu wrote:

    package main

    /*
    typedef union _B{
    int b;
    int c;
    }B;
    typedef struct _A{
    int a;
    B b;
    }A;

    */
    import "C"

    func main() {
    var a C.A
    a.a = 0
    a.b.b = 0
    }


    a.b.b will not be found in struct A while building.
    It works OK in c compiler like gcc.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 15, '13 at 7:11a
activeSep 16, '13 at 2:45a
posts3
users2
websitegolang.org

2 users in discussion

Allan Xu: 2 posts Carlos Castillo: 1 post

People

Translate

site design / logo © 2021 Grokbase