Skip to content

Instantly share code, notes, and snippets.

@hectorgool
Created September 13, 2015 19:46
Show Gist options
  • Select an option

  • Save hectorgool/4af7a982a26721b2be4e to your computer and use it in GitHub Desktop.

Select an option

Save hectorgool/4af7a982a26721b2be4e to your computer and use it in GitHub Desktop.
Go API
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
)
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{HelloMessage}
output, err := json.Marshal(message)
if err != nil {
fmt.Println("Something went wrong!")
}
fmt.Fprintf(w, string(output))
}
func main() {
gorillaRoute := mux.NewRouter()
gorillaRoute.HandleFunc("/api/{user:[0-9]+}", Hello)
http.Handle("/", gorillaRoute)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment