FAQ
I'm attempting to use cgo in order to make a Go wrapper around a C event
handling library, and am running into some naming collisions. To use this
library, the Go code needs to look something like:

var event C.Event
var done bool

for !done {
switch event.type {
}
}

The problem here is that the "type" in the switch statement is being
detected as a go keyword rather than a field in the event struct. Does
anybody know how to get around this limitation?

Thanks,
Chris Covert

Search Discussions

  • Steve McCoy at Sep 3, 2012 at 7:38 pm
    Try "event._type".

    On Monday, September 3, 2012 3:04:54 PM UTC-4, cover...@gmail.com wrote:

    I'm attempting to use cgo in order to make a Go wrapper around a C event
    handling library, and am running into some naming collisions. To use this
    library, the Go code needs to look something like:

    var event C.Event
    var done bool

    for !done {
    switch event.type {
    }
    }

    The problem here is that the "type" in the switch statement is being
    detected as a go keyword rather than a field in the event struct. Does
    anybody know how to get around this limitation?

    Thanks,
    Chris Covert
  • Covertops5 at Sep 3, 2012 at 7:28 pm
    I'm getting a type "_CType_Event has no field or method _type" when I
    switch to event._type.
    On Monday, September 3, 2012 3:15:09 PM UTC-4, Steve McCoy wrote:

    Try "event._type".

    On Monday, September 3, 2012 3:04:54 PM UTC-4, cover...@gmail.com wrote:

    I'm attempting to use cgo in order to make a Go wrapper around a C event
    handling library, and am running into some naming collisions. To use this
    library, the Go code needs to look something like:

    var event C.Event
    var done bool

    for !done {
    switch event.type {
    }
    }

    The problem here is that the "type" in the switch statement is being
    detected as a go keyword rather than a field in the event struct. Does
    anybody know how to get around this limitation?

    Thanks,
    Chris Covert
  • Steve McCoy at Sep 3, 2012 at 7:54 pm
    I actually just ran into this, too. Is C.Event a union? I got around it
    with code like this, but I'm suspicious of how correct it is:

    q := (*C.QuitEvent) (unsafe.Pointer(&event))
    switch q._type {
    ...
    }

    Here, QuitEvent would be one of the members of the Event union.
    On Monday, September 3, 2012 3:28:09 PM UTC-4, cover...@gmail.com wrote:

    I'm getting a type "_CType_Event has no field or method _type" when I
    switch to event._type.
    On Monday, September 3, 2012 3:15:09 PM UTC-4, Steve McCoy wrote:

    Try "event._type".

    On Monday, September 3, 2012 3:04:54 PM UTC-4, cover...@gmail.com wrote:

    I'm attempting to use cgo in order to make a Go wrapper around a C event
    handling library, and am running into some naming collisions. To use this
    library, the Go code needs to look something like:

    var event C.Event
    var done bool

    for !done {
    switch event.type {
    }
    }

    The problem here is that the "type" in the switch statement is being
    detected as a go keyword rather than a field in the event struct. Does
    anybody know how to get around this limitation?

    Thanks,
    Chris Covert
  • Steve McCoy at Sep 3, 2012 at 7:57 pm
    And of course QuitEvent had to have a type field. I don't know what you're
    interfacing with, but I'm using SDL2 and may have just got lucky with how
    the member structs are defined.
    On Monday, September 3, 2012 3:47:09 PM UTC-4, Steve McCoy wrote:

    I actually just ran into this, too. Is C.Event a union? I got around it
    with code like this, but I'm suspicious of how correct it is:

    q := (*C.QuitEvent) (unsafe.Pointer(&event))
    switch q._type {
    ...
    }

    Here, QuitEvent would be one of the members of the Event union.
    On Monday, September 3, 2012 3:28:09 PM UTC-4, cover...@gmail.com wrote:

    I'm getting a type "_CType_Event has no field or method _type" when I
    switch to event._type.
    On Monday, September 3, 2012 3:15:09 PM UTC-4, Steve McCoy wrote:

    Try "event._type".

    On Monday, September 3, 2012 3:04:54 PM UTC-4, cover...@gmail.com wrote:

    I'm attempting to use cgo in order to make a Go wrapper around a C
    event handling library, and am running into some naming collisions. To use
    this library, the Go code needs to look something like:

    var event C.Event
    var done bool

    for !done {
    switch event.type {
    }
    }

    The problem here is that the "type" in the switch statement is being
    detected as a go keyword rather than a field in the event struct. Does
    anybody know how to get around this limitation?

    Thanks,
    Chris Covert
  • Sebastien Binet at Sep 3, 2012 at 8:08 pm
    Chris,
    On Mon, Sep 3, 2012 at 9:04 PM, wrote:
    I'm attempting to use cgo in order to make a Go wrapper around a C event
    handling library, and am running into some naming collisions. To use this
    library, the Go code needs to look something like:

    var event C.Event
    var done bool

    for !done {
    switch event.type {
    }
    }

    The problem here is that the "type" in the switch statement is being
    detected as a go keyword rather than a field in the event struct. Does
    anybody know how to get around this limitation?
    I also encountered that issue when wrapping libffi.
    I worked around this by writing a little C-shim:
    https://github.com/sbinet/go-ffi/blob/master/pkg/ffi/type.go#L14

    which I'd used like so:
    https://github.com/sbinet/go-ffi/blob/master/pkg/ffi/type.go#L187

    hth,
    -s
  • Covertops5 at Sep 3, 2012 at 9:38 pm
    I was actually using SDL as well. The C-shim technique worked great (I
    didn't end up trying the other suggestions, since this seemed a bit
    cleaner). I had no idea you could even do that in the cgo import
    comments... that's pretty nifty.

    Thanks,
    Chris Covert
    On Monday, September 3, 2012 3:04:54 PM UTC-4, cover...@gmail.com wrote:

    I'm attempting to use cgo in order to make a Go wrapper around a C event
    handling library, and am running into some naming collisions. To use this
    library, the Go code needs to look something like:

    var event C.Event
    var done bool

    for !done {
    switch event.type {
    }
    }

    The problem here is that the "type" in the switch statement is being
    detected as a go keyword rather than a field in the event struct. Does
    anybody know how to get around this limitation?

    Thanks,
    Chris Covert
  • Steve McCoy at Sep 3, 2012 at 10:05 pm
    Yeah, it's a good technique and I've changed my own code accordingly. I was
    too tripped up on something surprising, I guess. Thanks, Sebastien!
    On Monday, September 3, 2012 5:37:56 PM UTC-4, cover...@gmail.com wrote:

    I was actually using SDL as well. The C-shim technique worked great (I
    didn't end up trying the other suggestions, since this seemed a bit
    cleaner). I had no idea you could even do that in the cgo import
    comments... that's pretty nifty.

    Thanks,
    Chris Covert
    On Monday, September 3, 2012 3:04:54 PM UTC-4, cover...@gmail.com wrote:

    I'm attempting to use cgo in order to make a Go wrapper around a C event
    handling library, and am running into some naming collisions. To use this
    library, the Go code needs to look something like:

    var event C.Event
    var done bool

    for !done {
    switch event.type {
    }
    }

    The problem here is that the "type" in the switch statement is being
    detected as a go keyword rather than a field in the event struct. Does
    anybody know how to get around this limitation?

    Thanks,
    Chris Covert

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 3, '12 at 7:13p
activeSep 3, '12 at 10:05p
posts8
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase