Created
June 17, 2020 06:56
-
-
Save velotiotech/a94e64b2c8f8eb2c4f81071b76b67085 to your computer and use it in GitHub Desktop.
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" | |
"net/http" | |
"log" | |
"github.com/gorilla/mux" | |
) | |
type Message struct { | |
Msg string | |
} | |
func helloWorldJSON(w http.ResponseWriter, r *http.Request) { | |
m := Message{"Hello World"} | |
response, _ := json.Marshal(m) | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusOK) | |
w.Write(response) | |
} | |
func main() { | |
r := mux.NewRouter() | |
r.HandleFunc("/hello", helloWorldJSON).Methods("GET") | |
if err := http.ListenAndServe(":8080", r); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment