title: Type cast custom types to base types
body: <p>How can I convert custom type to <code>interface{}</code> and then to base type (ex. <code>uint8</code>)?</p>
  
  <p>I can't use direct cast like <code>uint16(val.(Year))</code> because I may not know all custom types, but I can determinate base types (<code>uint8</code>, <code>uint32</code>,...) in runtime</p>
  
  <hr>
  
  <p>There are many custom types (usually used as enums) based on numeric:</p>
  
  <p>ex:</p>
  
  <pre><code>type Year  uint16
  type Day   uint8
  type Month uint8
  </code></pre>
  
  <p>and so on...</p>
  
  <p>The question is about type casting from <code>interface{}</code> to base types:</p>
  
  <pre><code>package main
  
  import "fmt"
  
  type Year uint16
  
  // ....
  //Many others custom types based on uint8
  
  func AsUint16(val interface{}) uint16 {
      return val.(uint16) //FAIL:  cannot convert val (type interface {}) to type uint16: need type assertion
  }
  
  func AsUint16_2(val interface{}) uint16 {
      return uint16(val) //FAIL:   cannot convert val (type interface {}) to type uint16: need type assertion
  }
  
  func main() {
      fmt.Println(AsUint16_2(Year(2015)))
  }
  </code></pre>
  
  <p><a href="http://play.golang.org/p/cyAnzQ90At">http://play.golang.org/p/cyAnzQ90At</a></p>