Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 17, 2020 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velotiotech/a94e64b2c8f8eb2c4f81071b76b67085 to your computer and use it in GitHub Desktop.
Save velotiotech/a94e64b2c8f8eb2c4f81071b76b67085 to your computer and use it in GitHub Desktop.
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