title: Map of types in Golang?
body: <p>I am writing a parser for audio/video stream which is known to be comprised of frames of several certain types. Each frame type has its own subheader format, so I define a struct type for each to use them for variables passed as the 3rd argument to <a href="https://pkg.go.dev/encoding/binary#Read" rel="nofollow noreferrer">binary.Read</a>:</p>
  <pre class="lang-go prettyprint-override"><code>type TypeVideoIHeader struct {
      MType       byte // type of media e.g. H.264
      FPS         byte
      Width       byte
      Height      byte
      DateTime    int32
      Length      int32
  }
  
  type TypeVideoPHeader struct {
      Length      int32
  }
  
  type TypeAudioHeader struct {
      MType       byte
      SampleRate  byte
      Length      int16
  }
  </code></pre>
  <p>Which type a frame is is defined by a certain byte in its header, so I put those into constants:</p>
  <pre class="lang-go prettyprint-override"><code>type FrameType byte
  
  const (
      VideoI  FrameType = 0xFC
      VideoP  FrameType = 0xFD
      Audio   FrameType = 0xFA
  )
  </code></pre>
  <p>Now, before I can call <code>binary.Read</code> <strong>I have to create a variable of the right type</strong> (one of the 3 structs above) which the function will fill with values from the stream.</p>
  <p>How do I initialise a variable of the right type just by a <code>FrameType</code> variable? Is there a concise and elegant solution?</p>
  <p>Say if this kind of trick was possible:</p>
  <pre class="lang-go prettyprint-override"><code>TMap := map[FrameType]type{
      VideoI: TypeVideoIHeader,
      VideoP: TypeVideoPHeader,
      Audio: TypeAudioHeader,
  }
  
  var videoISubHeader TMap[VideoI]
  </code></pre>
  <p>— that would be sort of what I am after.</p>