Created
July 7, 2018 19:54
-
-
Save gufranmirza/b7c2614e7c8fe76845b69b3140bc3e18 to your computer and use it in GitHub Desktop.
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 ( | |
"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