Last active
December 31, 2015 02:59
-
-
Save congjf/7924595 to your computer and use it in GitHub Desktop.
JSON in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON in Golang |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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