FAQ
I am trying to write a little Go wrapper for the x264 library from VLC, and
that library has a bunch of quite large and complicated structs that I need
to interface with.

While trying to do so I ran into a problem that I almost feel is a problem
with the cgo package itself. I've boiled it down to a pretty minimal
example to show what I mean, and it can be demonstrated with just a single
.h file and a main Go file.

A brief explanation and more details in the code below. When trying to use
a C struct that has other structs inside of it (anonymous structs) it is as
if cgo stops parsing the struct past that first nested struct. However it
still finds all the fields inside that first nested struct, but it puts
them all at the top level of the original struct. Anything past the first
nested struct doesn't get read at all, and when trying to print the object
they all just get passed in to what seems to be an array of raw bytes.

Hopefully the code below is clear enough.

Does anyone know what's going on here or how to circumvent the problem?


struct.h
---
typedef struct param_struct_t {
   int a;
   int b;
   struct {
     int c;
     int d;
   } anon;
   int e;
   struct {
     int f;
     int g;
   } anon2;
} param_struct_t;

main.go
---
package main

/*
#include "struct.h"
*/
import "C"
import (
   "fmt"
)

func main() {
   var param C.param_struct_t
   fmt.Println(param.a) // Works and should work
   fmt.Println(param.b) // Works and should work
   fmt.Println(param.c) // Works fine but shouldn't work
   fmt.Println(param.d) // Works fine but shouldn't work
   // fmt.Println(param.e) // Produces type error: ./main.go:17: param.e
undefined (type C.param_struct_t has no field or method e)
   // fmt.Println(param.anon) // Produces type error: ./main.go:18:
param.anon undefined (type C.param_struct_t has no field or method anon)

   // The following shows that the first parameters and the parameters from
the
   // first anonymous struct gets read properly, but the subsequent things
are
   // read as seemingly raw bytes.
   fmt.Printf("%#v", param) // Prints out: main._Ctype_param_struct_t{a:0,
b:0, c:0, d:0, _:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0}}
}


--
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

  • Isoftevo at Sep 18, 2013 at 3:10 pm
    Replying to myself here, I posted the question on Stackoverflow as well and
    someone suggested that it might be a problem with my go version. I was
    running Go 1.2rc2 and so I tried downgrading to go 1.1.2 and now it works
    fine. Could this be a bug with go 1.2?

    Regards
    On Wednesday, September 18, 2013 8:42:22 AM UTC+2, iq...@3h.se wrote:

    I am trying to write a little Go wrapper for the x264 library from VLC,
    and that library has a bunch of quite large and complicated structs that I
    need to interface with.

    While trying to do so I ran into a problem that I almost feel is a problem
    with the cgo package itself. I've boiled it down to a pretty minimal
    example to show what I mean, and it can be demonstrated with just a single
    .h file and a main Go file.

    A brief explanation and more details in the code below. When trying to use
    a C struct that has other structs inside of it (anonymous structs) it is as
    if cgo stops parsing the struct past that first nested struct. However it
    still finds all the fields inside that first nested struct, but it puts
    them all at the top level of the original struct. Anything past the first
    nested struct doesn't get read at all, and when trying to print the object
    they all just get passed in to what seems to be an array of raw bytes.

    Hopefully the code below is clear enough.

    Does anyone know what's going on here or how to circumvent the problem?


    struct.h
    ---
    typedef struct param_struct_t {
    int a;
    int b;
    struct {
    int c;
    int d;
    } anon;
    int e;
    struct {
    int f;
    int g;
    } anon2;
    } param_struct_t;

    main.go
    ---
    package main

    /*
    #include "struct.h"
    */
    import "C"
    import (
    "fmt"
    )

    func main() {
    var param C.param_struct_t
    fmt.Println(param.a) // Works and should work
    fmt.Println(param.b) // Works and should work
    fmt.Println(param.c) // Works fine but shouldn't work
    fmt.Println(param.d) // Works fine but shouldn't work
    // fmt.Println(param.e) // Produces type error: ./main.go:17: param.e
    undefined (type C.param_struct_t has no field or method e)
    // fmt.Println(param.anon) // Produces type error: ./main.go:18:
    param.anon undefined (type C.param_struct_t has no field or method anon)

    // The following shows that the first parameters and the parameters from
    the
    // first anonymous struct gets read properly, but the subsequent things
    are
    // read as seemingly raw bytes.
    fmt.Printf("%#v", param) // Prints out: main._Ctype_param_struct_t{a:0,
    b:0, c:0, d:0, _:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0}}
    }

    --
    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.
  • Pranay Kanwar at Sep 18, 2013 at 3:10 pm
    What version of go are you using. Works as intended with go 1.1.2

    Example produces error
    $ go build main.go
    # command-line-arguments
    main.go:15: param.c undefined (type C.param_struct_t has no field or method
    c)
    main.go:16: param.d undefined (type C.param_struct_t has no field or method
    d)

    The following works

    fmt.Println(param.anon.c)
    param.anon.c = 10
    fmt.Println(param.anon)

    -pk
    On Wednesday, September 18, 2013 12:12:22 PM UTC+5:30, iq...@3h.se wrote:

    I am trying to write a little Go wrapper for the x264 library from VLC,
    and that library has a bunch of quite large and complicated structs that I
    need to interface with.

    While trying to do so I ran into a problem that I almost feel is a problem
    with the cgo package itself. I've boiled it down to a pretty minimal
    example to show what I mean, and it can be demonstrated with just a single
    .h file and a main Go file.

    A brief explanation and more details in the code below. When trying to use
    a C struct that has other structs inside of it (anonymous structs) it is as
    if cgo stops parsing the struct past that first nested struct. However it
    still finds all the fields inside that first nested struct, but it puts
    them all at the top level of the original struct. Anything past the first
    nested struct doesn't get read at all, and when trying to print the object
    they all just get passed in to what seems to be an array of raw bytes.

    Hopefully the code below is clear enough.

    Does anyone know what's going on here or how to circumvent the problem?


    struct.h
    ---
    typedef struct param_struct_t {
    int a;
    int b;
    struct {
    int c;
    int d;
    } anon;
    int e;
    struct {
    int f;
    int g;
    } anon2;
    } param_struct_t;

    main.go
    ---
    package main

    /*
    #include "struct.h"
    */
    import "C"
    import (
    "fmt"
    )

    func main() {
    var param C.param_struct_t
    fmt.Println(param.a) // Works and should work
    fmt.Println(param.b) // Works and should work
    fmt.Println(param.c) // Works fine but shouldn't work
    fmt.Println(param.d) // Works fine but shouldn't work
    // fmt.Println(param.e) // Produces type error: ./main.go:17: param.e
    undefined (type C.param_struct_t has no field or method e)
    // fmt.Println(param.anon) // Produces type error: ./main.go:18:
    param.anon undefined (type C.param_struct_t has no field or method anon)

    // The following shows that the first parameters and the parameters from
    the
    // first anonymous struct gets read properly, but the subsequent things
    are
    // read as seemingly raw bytes.
    fmt.Printf("%#v", param) // Prints out: main._Ctype_param_struct_t{a:0,
    b:0, c:0, d:0, _:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0}}
    }

    --
    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.
  • Minux at Sep 21, 2013 at 1:12 am

    On Wed, Sep 18, 2013 at 2:42 AM, wrote:

    I am trying to write a little Go wrapper for the x264 library from VLC,
    and that library has a bunch of quite large and complicated structs that I
    need to interface with.

    While trying to do so I ran into a problem that I almost feel is a problem
    with the cgo package itself. I've boiled it down to a pretty minimal
    example to show what I mean, and it can be demonstrated with just a single
    .h file and a main Go file.

    A brief explanation and more details in the code below. When trying to use
    a C struct that has other structs inside of it (anonymous structs) it is as
    if cgo stops parsing the struct past that first nested struct. However it
    still finds all the fields inside that first nested struct, but it puts
    them all at the top level of the original struct. Anything past the first
    nested struct doesn't get read at all, and when trying to print the object
    they all just get passed in to what seems to be an array of raw bytes.

    Hopefully the code below is clear enough.

    Does anyone know what's going on here or how to circumvent the problem?
    reproduced at tip, and it's a regress from Go 1.1, so please file an issue,
    thanks.
    struct.h
    ---
    typedef struct param_struct_t {
    int a;
    int b;
    struct {
    int c;
    int d;
    } anon;
    int e;
    struct {
    int f;
    int g;
    } anon2;
    } param_struct_t;

    main.go
    ---
    package main

    /*
    #include "struct.h"
    */
    import "C"
    import (
    "fmt"
    )

    func main() {
    var param C.param_struct_t
    fmt.Println(param.a) // Works and should work
    fmt.Println(param.b) // Works and should work
    fmt.Println(param.c) // Works fine but shouldn't work
    fmt.Println(param.d) // Works fine but shouldn't work
    // fmt.Println(param.e) // Produces type error: ./main.go:17: param.e
    undefined (type C.param_struct_t has no field or method e)
    // fmt.Println(param.anon) // Produces type error: ./main.go:18:
    param.anon undefined (type C.param_struct_t has no field or method anon)

    // The following shows that the first parameters and the parameters from
    the
    // first anonymous struct gets read properly, but the subsequent things
    are
    // read as seemingly raw bytes.
    fmt.Printf("%#v", param) // Prints out: main._Ctype_param_struct_t{a:0,
    b:0, c:0, d:0, _:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    0x0, 0x0, 0x0}}
    }
    --
    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 18, '13 at 7:02a
activeSep 21, '13 at 1:12a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase