Basic Go HTTP router
Instead of case x == true you can use boolean expressions directly,
and write simply:

case x:


The same regex is written twice here.
What's worse is that the pattern matching is also evaluated twice:


case match("^/hello/([^/]+)$", route) == true:
    var re = regexp.MustCompile("^/hello/([^/]+)$")



I'm not sure if there's an elegant solution for this situation in Go.
But at the minimum you could move the regex pattern into a variable to avoid typing it twice.

Another alternative is to create a matcher structure,
with a match function that will not only match a pattern and return a boolean,
but at the same time also store the sub-string you want to extract.

type matcher struct {
    value string
}

func (m *matcher) match(pattern string, route string) bool {
    var re = regexp.MustCompile(pattern)
    matches := re.FindStringSubmatch(route)
    if len(matches) &lt; 2 {
        return false
    }

    m.value = matches[1]

    return true
}


With the help of this, you could rewrite the route function as:

func route(w http.ResponseWriter, r *http.Request) {
    route := r.URL.Path
    m := &amp;matcher{}
    switch {
    case route == "/":
        home(w, r)
    case route == "/hello" &amp;&amp; r.Method == "GET":
        hello(w, r)
    case m.match("^/hello/([^/]+)$", route):
        helloName(w, r, m.value)
    default:
        notFound(w, r)
    }
}