Go Lang server template
This is a sample template to build a server in Go using net/http
package and
gorilla mux
as muxer
Use Git to clone the repo:
$ git clone https://github.com/mukeshm/goserveit.git
This will clone the goserveit into your local filesystem
# switch to your local repo
cd goserveit
go run *.go
This will run a server on default port 8080
, so we can open http://localhost:8080
in browser
Handlers for the new routes can be added in handlers.go
file like :
func myHandler(w http.ResponseWriter, r *http.Request) {
//Do what ever you want
}
Once defining the handlers in handlers.go
file, we have to map those handlers to routes. We can just add the mapping in routes.go
like:
var routes = Routes{
Route{
"/",
http.MethodGet,
indexHandler,
"Index",
},
Route{
"/hello", //defining a pattern to match path
http.MethodGet, //for which HTTP method
myHandler, //handler function to call for the path if matched
"HelloHandler", //name - not used, just for clarification
},
}
Change the default port number 8080
to whatever valid port number in main
function in server.go