You can do this using standard libraries or other routers. I will introduce two methods:
Standard library:
Import (
fmt
net//Julien Schmidt//Julien Schmidt//http
log
)
functionlocationhandler(whttp。 ResponseWriter。 Request, pshttprouter Params){
fmt。 Fprintf(w, location: %s\n, ps. Alias (location))
}
funcmain(){
Router: = httprouter. New ()
Router. GET(/location/:loc,LocationHandler)
Journal. Fatal (http. ListenAndServe(:8080, router))
}
Note that httprouter uses a slightly different signature for the handler. As you can see, this is because it also passes these parameters to the function.
Oh, one more thing to note, you can click it directly on http://localhost: 8080/location/titanrolex with your browser-if something else is good enough, it will encode the URLEncode as http://localhost: 8080/location/titan% 20 Rolex.