Last active
August 29, 2015 14:03
-
-
Save chilijung/b64cf2dfbe6dd5c882a6 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"encoding/json" | |
) | |
type Response map[string]interface{} | |
func (r Response) String() (s string) { | |
b, err := json.Marshal(r) | |
if err != nil { | |
s = "" | |
return | |
} | |
s = string(b) | |
return | |
} | |
func myHandler (w http.ResponseWriter, req *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
fmt.Fprint(w, Response{"success": true, "message": "Hello"}) | |
return | |
} | |
func main() { | |
http.HandleFunc("/", myHandler) | |
err := http.ListenAndServe(":3000", nil) | |
if err != nil { | |
log.Fatal("Listen and serve error: ", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment