For single use:
func oneHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/one", http.StatusMovedPermanently) }
If this happens several times, you can create a redirect handler:
func redirectHandler(path string) func(http.ResponseWriter, *http.Request) { return func (w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, path, http.StatusMovedPermanently) } }
and use it as follows:
func init() { http.HandleFunc("/one", oneHandler) http.HandleFunc("/1", redirectHandler("/one")) http.HandleFunc("/two", twoHandler) http.HandleFunc("/2", redirectHandler("/two"))
source share