Created
November 1, 2014 21:08
-
-
Save cpliakas/c3bbf589dff3dbef6e1b to your computer and use it in GitHub Desktop.
Gorilla Mux return JSON payload
This file contains 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
package main | |
import ( | |
"log" | |
"net/http" | |
"github.com/bitly/go-simplejson" | |
"github.com/gorilla/mux" | |
) | |
func main() { | |
router := mux.NewRouter() | |
router.HandleFunc("/", GetRoot).Methods("GET") | |
http.Handle("/", router) | |
log.Println("Listening on :3000") | |
log.Fatal(http.ListenAndServe(":3000", nil)) | |
} | |
func GetRoot(w http.ResponseWriter, r *http.Request) { | |
json := simplejson.New() | |
json.Set("foo", "bar") | |
payload, err := json.MarshalJSON() | |
if err != nil { | |
log.Println(err) | |
} | |
w.Header().Set("Content-Type", "application/json") | |
w.Write(payload) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment