Skip to content

Instantly share code, notes, and snippets.

@codingjester
Created September 1, 2014 00:48
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 codingjester/c1948c71873bed37b8a6 to your computer and use it in GitHub Desktop.
Save codingjester/c1948c71873bed37b8a6 to your computer and use it in GitHub Desktop.
Simple Hello World Golang Web Application
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
}
Copy link

ghost commented Jul 20, 2017

A little late to the party, but you forgot to import "fmt" 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment