title: How can I access the fields of an interface in Go?
body: <p>I'm trying to do this:</p>
  
  <pre><code>if event.Type == sdl.QUIT {
      utils.Running = false
  }
  </code></pre>
  
  <p>But I can't because when I try to build, I get this error:</p>
  
  <pre><code> ./mm.go:11: event.Type undefined (type sdl.Event has no field or method Type)
  </code></pre>
  
  <p>Here is the relevant source code of the library I'm trying to use:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>As you can see, all of the other Events have the field <code>Type</code>. How can I access this?</p>
  
  <h1>Solution</h1>
  
  <p>This how I ended up polling events in <a href="https://github.com/veandco/go-sdl2/" rel="nofollow">this SDL2 binding for Go</a>, in case anyone is wondering:</p>
  
  <pre><code>func PollEvents() {
      for {
          if event := sdl.PollEvent(); event != nil {
              switch event.(type) {
              case *sdl.QuitEvent:
                  utils.Running = false
              }
          } else {
              break
          }
      }
  }
  </code></pre>