Skip to content

Instantly share code, notes, and snippets.

@sonyarianto
Created December 5, 2018 09:40
Show Gist options
  • Save sonyarianto/45feb2da543ba038a6ae7413f496666e to your computer and use it in GitHub Desktop.
Save sonyarianto/45feb2da543ba038a6ae7413f496666e to your computer and use it in GitHub Desktop.
Web routing with default mux (net/http)
package main
import (
"io"
"log"
"net/http"
)
func serviceDefault(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
io.WriteString(w, "this is the / path")
}
func service1(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "this is the /service1 path")
}
func service2(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "this is the /service2 path")
}
func main() {
http.HandleFunc("/", serviceDefault)
http.HandleFunc("/service1", service1)
http.HandleFunc("/service2", service2)
log.Fatal(http.ListenAndServe(":3000", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment