FAQ
Hi guys, I have issue with accessing field with name "type"

error: event._type undefined (type C.SDL_Event has no field or method _type)

where C.SDL_Event is union type

typedef union SDL_Event {
  Uint8 type;
  …
} SDL_Event;

How to access that field?


I'm wrapping SDL 1.2.15-3 and go version go1.1.1 linux/amd64.

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

  • Sebastien Binet at Jul 7, 2013 at 8:33 am
    Martin,
    On Sat, Jul 6, 2013 at 9:04 PM, Martin Drlík wrote:
    Hi guys, I have issue with accessing field with name "type"

    error: event._type undefined (type C.SDL_Event has no field or method _type)

    where C.SDL_Event is union type

    typedef union SDL_Event {
    Uint8 type;

    } SDL_Event;

    How to access that field?
    I usually write a little accessor function in C.

    // #include <sdl.h>
    // Uint8 _go_sdlevent_get_type(SDL_Event *evt) {
    // return evt->type;
    // }
    import "C"

    hth,
    -s

    --
    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.
  • Matthew Kane at Jul 7, 2013 at 3:26 pm
    According to the manual, you can prefix it with an underscore.
    On Jul 6, 2013 3:05 PM, "Martin Drlík" wrote:

    Hi guys, I have issue with accessing field with name "type"

    error: event._type undefined (type C.SDL_Event has no field or method
    _type)

    where C.SDL_Event is union type

    typedef union SDL_Event {
    Uint8 type;

    } SDL_Event;

    How to access that field?


    I'm wrapping SDL 1.2.15-3 and go version go1.1.1 linux/amd64.

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

    --
    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 Jul 7, 2013 at 3:54 pm

    On Sun, Jul 7, 2013 at 11:26 PM, Matthew Kane wrote:

    According to the manual, you can prefix it with an underscore.
    only if the name is a field of a C struct.

    you can't access C's global variable (or const, macro) named type in Go
    directly
    through C._type.

    cmd/cgo's document is somewhat inaccurate in this regard, it says:
    " Within the Go file, C identifiers or field names that are keywords in
    Go
         can be accessed by prefixing them with an underscore: if x points at a C
         struct with a field named "type", x._type accesses the field.
    "

    however, this program won't compile (i've tested Go 1.0.3, Go 1.1 and Go
    tip):
    package f
    // int type() { return 0; }
    import "C"
    func _() C.int { return C._type() }

    $ go build x.go
    # command-line-arguments
    error: '_type' undeclared (first use in this function)
    error: (Each undeclared identifier is reported only once

    i think the document is incorrect, as what if there is _type variable along
    side a variable
    named type in C? if _type is used for "type" one, how to reference the
    other one?

    i will propose a CL to fix the cgo document in Go 1.2.

    --
    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 Jul 7, 2013 at 3:41 pm

    On Sun, Jul 7, 2013 at 3:04 AM, Martin Drlík wrote:

    Hi guys, I have issue with accessing field with name "type"

    error: event._type undefined (type C.SDL_Event has no field or method
    _type)

    where C.SDL_Event is union type

    typedef union SDL_Event {
    Uint8 type;

    } SDL_Event;

    How to access that field?


    I'm wrapping SDL 1.2.15-3 and go version go1.1.1 linux/amd64.
    the problem is that SDL_Event is an union.
    if it were a struct, you'd have no problem to reference the _type field in
    Go.

    please note that Go doesn't support generic C unions, so cgo will recognize
    any C union as a byte array in Go, thus either you write C wrapper functions
    or access fields through appropriate offset in the byte array.

    (for example, union { char type; int type2; } is treated as [4]byte in Go)

    --
    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
postedJul 6, '13 at 7:04p
activeJul 7, '13 at 3:54p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase