Skip to content

Instantly share code, notes, and snippets.

@congjf
Last active December 31, 2015 02:59
Show Gist options
  • Save congjf/7924595 to your computer and use it in GitHub Desktop.
Save congjf/7924595 to your computer and use it in GitHub Desktop.
JSON in Golang
JSON in Golang
import (
"net/http"
"encoding/json"
"fmt"
)
type test struct {
Test string `json:"test"`
}
func (handler Handlers) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var t test
err := decoder.Decode(&t)
if err != nil {
panic(err.Error())
}
fmt.Fprintf(rw, t.Test)
}
func (u *Organizations) Index(rw http.ResponseWriter, req *http.Request) {
// some process...
encoder := json.NewEncoder(rw)
// result is a STRUCT what you want to ENCODE and response to front.
encoder.Encode(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment