Skip to content

Instantly share code, notes, and snippets.

@gufranmirza
Created July 7, 2018 19:54
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 gufranmirza/b7c2614e7c8fe76845b69b3140bc3e18 to your computer and use it in GitHub Desktop.
Save gufranmirza/b7c2614e7c8fe76845b69b3140bc3e18 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
// The new router function creates the router and
// returns it to us. We can now use this function
// to instantiate and test the router outside of the main function
func newRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/hello", handler).Methods("GET")
return r
}
func main() {
// The router is now formed by calling the `newRouter` constructor function
// that we defined above. The rest of the code stays the same
r := newRouter()
http.ListenAndServe(":8080", r)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment