title: How to validate JSON input using Golang?
body: <p>I'm using Beego framework to build a web application in Go. I have to validate the incoming JSON in an API request. </p>
  
  <p>I can unmarshal the JSON into a struct which works fine, but I want to validate the data as well. For example, if the type doesn't match with the type in struct json.Unmarshal will reutrn an error on the first occurence. I want to validate and get all the errors at once for JSON.</p>
  
  <p>I've tried <code>https://github.com/thedevsaddam/govalidator</code> but the package needs a reference to request object which is not available in the controller of Beego. There are other validators which can validate a struct but i want the json validation as well.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>A reference to the request object in beego can be found from the controller's context object as:</p>
  
  <pre><code>func (this *MainController) Post() {
      fmt.Println(this.Ctx.Request)
  }
  </code></pre>
  
  <p><strong>But</strong> the problem remains same with json unmarshal. If there's any slight mismatch in the type, json.unmarshal will immediately panic. I want to be able to validate the type as well.</p>