mardi 24 février 2015

How does one send a [][]byte in Golang to the browser to be decoded as an image

In my back-end golang web server I have converted and processed a directory of images that i have read in using os.ReadDir


These images are stored as a [][]byte. I want to be able to send these images through a GET request to be displayed in the browser using Javascript.


I am having trouble figuring out how to begin the process to send the data from the Golang web server. The resources I am currently using are the typical net/http package, and Gorilla Mux/Websockets.


Here is some sample code that shows how I am currently doing a get request which return some json. How can I similarly send a [][]byte array instead of rendering a template or JSON?



import (
"html/template"
"log"
"net/http"
"encoding/json"
"http://ift.tt/J9WoXt"
)

func ViewSample(rw http.ResponseWriter, req *http.Request) {
type Sample struct {
Id int `json:"id"`
Name string `json:"name"`
User string `json:"user
}

params := mux.Vars(req)
sampleId := params["id"]

sample := Sample{
Id: 3,
Name: "test",
User: "testuser"
}

json.NewEncoder(rw).Encode(sample)
}




Aucun commentaire:

Enregistrer un commentaire