dongshetao1814
dongshetao1814
2016-04-11 16:20

如何在Go中访问界面的字段?

已采纳

I'm trying to do this:

if event.Type == sdl.QUIT {
    utils.Running = false
}

But I can't because when I try to build, I get this error:

 ./mm.go:11: event.Type undefined (type sdl.Event has no field or method Type)

Here is the relevant source code of the library I'm trying to use:

type Event interface{}    

type CEvent struct {
    Type uint32
    _    [52]byte // padding
}

type CommonEvent struct {
    Type      uint32
    Timestamp uint32
}

// WindowEvent (https://wiki.libsdl.org/SDL_WindowEvent)
type WindowEvent struct {
    Type      uint32
    Timestamp uint32
    WindowID  uint32
    Event     uint8
    _         uint8 // padding
    _         uint8 // padding
    _         uint8 // padding
    Data1     int32
    Data2     int32
}

As you can see, all of the other Events have the field Type. How can I access this?

Solution

This how I ended up polling events in this SDL2 binding for Go, in case anyone is wondering:

func PollEvents() {
    for {
        if event := sdl.PollEvent(); event != nil {
            switch event.(type) {
            case *sdl.QuitEvent:
                utils.Running = false
            }
        } else {
            break
        }
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douyue8685 douyue8685 5年前

    You actually can't. Interfaces only define a method set that is available on a type, they do nothing to expose fields. In your case I would recommend doing a type switch. It would look a little like this;

         switch v := myInstance.(type) {
                    case CEvent:
                            fmt.Println(v)
                    case CommonEvent:
                            fmt.Println(v)
                    case WindowEvent:
                            fmt.Println(v)
                    default:
                            fmt.Println("unknown")
            }
    

    You may want to structure your code a bit differently depending on what you're doing with the instance after this but that gives you the basic idea. You can also do a type assertion with a single type like; v, err := myInstance.(CommonEvent) but I doubt it would be as effective here. It also returns an error if the type of myInstance is not CommonEvent so it's not really the best way to go about figuring out what type and interface instance may be.

    点赞 评论 复制链接分享
  • douxiong2001 douxiong2001 5年前

    You can spend a lot of time doing reflection calls or trying to guess the type or using type switches.

    Or you can just define an interface with functions that return the information you need.

    For example you could do

    type Event interface {
        GetCommonEvent() *CommonEvent
        GetWindowEvent() *WindowEvent
    }
    
    点赞 评论 复制链接分享
  • dream_life5200 dream_life5200 5年前

    You will need to know the type. Let's say we know it's a CEvent:

    cEvent, ok := Event.(CEvent)
    if !ok {
        // You lied, not a CEvent
        return
    }
    
    // Otherwise, you can get the type!
    fmt.Println(cEvent.Type)
    

    Of course if you don't know the type, you can keep type asserting until you get it right. Otherwise, throw an error, return a default value, etc:

    func getType(i interface{}) uint32 {
        cEvent, ok := i.(CEvent)
        if ok {
            return cEvent.Type
        }
    
        commonEvent, ok := i.(CommonEvent)
        if ok {
            return commonEvent.Type
        }
    
        // Etc
    
        return <default>
    }
    
    点赞 评论 复制链接分享

为你推荐