title: Golang convert base64 data into image on the fly and send response
body: <p>I have a sample base64 data, that can be used in <code>img</code> html tags like this:</p>
  
  <pre><code>&lt;img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAACklEQVR4nGNiAAAABgADNjd8qAAAAABJRU5ErkJggg==
  "/&gt;
  </code></pre>
  
  <p><a href="http://jsbin.com/suvuxigiwo/edit?html,output" rel="nofollow noreferrer">Demo </a></p>
  
  <p>And I am fiddling around with a small golang program.
  In golang, how do I convert this static base64 encoded data into an image on the fly and then send it as an image in response.</p>
  
  <p>Example:</p>
  
  <pre><code>http.HandleFunc("/getImage", imageHandler)
  
  func image(res http.ResponseWriter, req *http.Request) {
      data := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAACklEQVR4nGNiAAAABgADNjd8qAAAAABJRU5ErkJggg=="
      // Data is the base64 encoded image
      dec := b64.NewDecoder(b64.StdEncoding, data)
      res.Header().Set("Content-Type", "image/png")
      io.Copy(res, dec)
  }
  </code></pre>