Golang : Web routing/multiplex example
Routing based on the URL's path can be useful in some cases like build RESTful API server.
Problem :
You need to route/multiplex to different function/handler based on the URL's path. For example :
"/someresource/:id" ---> "code to do something with the resource"
"/users/:name/profile" ---> "code to do something with the profile"
Solution :
Use "net/http" NewServeMux() function. It compares incoming requests against a list of predefined URL paths, and calls the associated handler for the path whenever a match is found.
Code example ;
package main
import (
"net/http"
"strings"
)
func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func ReplyName(w http.ResponseWriter, r *http.Request) {
URISegments := strings.Split(r.URL.Path, "/")
w.Write([]byte(URISegments[1]))
}
func main() {
// http.Handler
mux := http.NewServeMux()
mux.HandleFunc("/", SayHelloWorld)
mux.HandleFunc("/replyname", ReplyName)
http.ListenAndServe(":8080", mux)
}
there are couple of third parties packages that provides more features when come to routing. For example, Gorilla Mux for path pattern matching (useful for RESTful APIs)
In this example, we will use Gorilla's Mux. Go get from github.com/gorilla/mux before trying out the codes below.
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
)
func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func ReplyNameGorilla(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"] // variable name is case sensitive
w.Write([]byte(fmt.Sprintf("Hello %s !", name)))
}
func main() {
mx := mux.NewRouter()
mx.HandleFunc("/", SayHelloWorld)
mx.HandleFunc("/{name}", ReplyNameGorilla) // variable name is case sensitive
http.ListenAndServe(":8080", mx)
}
Reference :
See also : Golang : Get URI segments by number and assign as variable example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.2k PHP : How to parse ElasticSearch JSON ?
+9.9k Golang : Text file editor (accept input from screen and save to file)
+5.7k AWS S3 : Prevent Hotlinking policy
+9.4k Golang : Detect number of active displays and the display's resolution
+28.3k Golang : Detect (OS) Operating System
+12k Golang : Validate email address
+7.7k Golang : Handle Palindrome string with case sensitivity and unicode
+9.9k Golang : How to tokenize source code with text/scanner package?
+12.1k Golang : Extract part of string with regular expression
+16.1k Golang : Convert slice to array
+11.9k Golang : md5 hash of a string
+4.6k Nginx and PageSpeed build from source CentOS example