Created
September 20, 2017 11:11
-
-
Save ara-ta3/1484851d780fb35988d957c8d4f4cc94 to your computer and use it in GitHub Desktop.
sample code using net/http in golang
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 ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
) | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | |
b := ResponseBody{ | |
OK: false, | |
Message: "Not Implemented", | |
} | |
j, e := json.Marshal(b) | |
if e != nil { | |
log.Fatalf("%+v", e) | |
} | |
fmt.Fprintln(w, string(j)) | |
}) | |
log.Fatal(http.ListenAndServe(":8080", mux)) | |
} | |
type ResponseBody struct { | |
OK bool `json:"ok"` | |
Message string `json:"message"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment