Created
September 13, 2015 19:46
-
-
Save hectorgool/4af7a982a26721b2be4e to your computer and use it in GitHub Desktop.
Go API
This file contains hidden or 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" | |
| "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