Created
September 1, 2014 00:48
-
-
Save codingjester/c1948c71873bed37b8a6 to your computer and use it in GitHub Desktop.
Simple Hello World Golang Web Application
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 ( | |
"net/http" | |
"github.com/gorilla/mux" | |
) | |
func main() { | |
r := mux.NewRouter() | |
r.HandleFunc("/{name[a-zA-Z]+}", RootHandler).Methods("GET") // Restricts "/:name" to only allow GETs | |
http.Handle("/", r) | |
http.ListenAndServe(":8080", nil) | |
} | |
func RootHandler(w http.ResponseWriter, r *http.Request) { | |
params := mux.Vars(r) // Parses the variables in URLS for extraction | |
name := params["name"] | |
hello := fmt.Sprintf("Hello, %s", name); | |
fmt.Fprintln(w, hello) // Prints as text/plain; More magic is needed for JSON | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little late to the party, but you forgot to import
"fmt"
👍