Skip to content

Instantly share code, notes, and snippets.

@dtoebe
Created January 3, 2017 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtoebe/a5c9512f19be2da4825ef51b875a01a1 to your computer and use it in GitHub Desktop.
Save dtoebe/a5c9512f19be2da4825ef51b875a01a1 to your computer and use it in GitHub Desktop.
GENERATING DATAURI IMAGES WITH GO - main.go - 3
//main.go
...
func main() {
...
for ... {...}
// Here we allocate and create a buffer that can easily be
// turned into a []byte
out := new(bytes.Buffer)
// We now encode the image we created to the buffer
err := png.Encode(out, img)
if err != nil {
// Handle errors
}
// This now takes a []byte of the buffer and base64 encodes it to a string
// Never needing to create the image file all done in memory
base64Img := base64.StdEncoding.EncodeToString(out.Bytes())
//And now you can see the magic happen.
// Go ahead and run it then take the output and copy/paste it in your
// browser's URL bar, and you'll see your image.
fmt.Println("data:image/png;base64,", base64Img)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment