Search code examples
golanguage-interoperabilitycgo

problems when accessing C union field


I'd like to access the field of C union in Go. below is my source code, but i got an error when compile it:

package main

// #include <stdio.h>
// #include <stdlib.h>
// union bar {
//        char   c;
//        int    i;
//        double d;
// };
import "C"

import "fmt"

func main() {
    var b *C.union_bar = new(C.union_bar)
    b.c = 4
    fmt.Println(b)
}

when i build, i got errors like below:

b.c undefined (type *[8]byte has no field or method c)

Who could tell me the correct approach to access a union field?


Solution

  • Seems like unions are treated, for type safety, as [N]byte, N == size of the biggest union item. Thus it is necessary to handle the "Go visible" type as [8]byte in this case. Then it appears to work:

    package main
    
    /*
    
    #include <stdio.h>
    #include <stdlib.h>
    union bar {
           char   c;
           int    i;
           double d;
    } bar;
    
    void foo(union bar *b) {
        printf("%i\n", b->i);
    };
    
    */
    import "C"
    
    import "fmt"
    
    func main() {
        b := new(C.union_bar)
        b[0] = 1
        b[1] = 2
        C.foo(b)
        fmt.Println(b)
    }
    

    (11:28) jnml@celsius:~/src/tmp/union$ go build && ./union
    513
    &[1 2 0 0 0 0 0 0]
    (11:28) jnml@celsius:~/src/tmp/union$ 
    

    Note: The same code would print a different number at a machine with other endianness.