12345678910111213141516171819202122232425262728293031 |
- package main
- import (
- "net/http"
- "encoding/json"
- "fmt"
- "github.com/gorilla/mux"
- )
- type API struct {
- Message string "json:message"
- }
- func Hello(w http.ResponseWriter, r *http.Request) {
- urlParams := mux.Vars(r)
- name := urlParams["user"]
- HelloMessage := "Hello, " + name
- message := API{Message:HelloMessage}
- output, err := json.Marshal(message)
- if err != nil {
- fmt.Println("Something went wrong", err)
- }
- fmt.Fprint(w, string(output))
- }
- func main() {
- gorillaRoute := mux.NewRouter()
- gorillaRoute.HandleFunc("/api/{user:[0-9]+}", Hello)
- http.Handle("/", gorillaRoute)
- http.ListenAndServe(":8080", nil)
- }
|