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
+5.7k Golang : List all packages and search for certain package
+4.5k JavaScript: Add marker function on Google Map
+20.6k Golang : Convert PNG transparent background image to JPG or JPEG image
+7.2k Golang : Process json data with Jason package
+35.9k Golang : How to split or chunking a file to smaller pieces?
+11.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+20.9k Golang : Clean up null characters from input data
+33.4k Golang : How to check if slice or array is empty?
+5.5k Swift : Get substring with rangeOfString() function example
+15.8k Golang : Get file permission
+41.2k Golang : Convert string to array/slice
+7.1k Golang : Of hash table and hash map