Fragment Description:

A useful facility:
http.Redirect signature:
func Redirect(w ResponseWriter, r *Request, urlStr string, code int) Redirect replies to the request with a redirect to url, which may be a path relative to the request path.
See also:
type Handler and the methods:
RedirectHandler signature:
func RedirectHandler(url string, code int) Handler RedirectHandler returns a request handler that redirects each request it receives to the given url using the given status code.


Last update, on 2015, Tue 1 Dec, 20:10:10

/* ... <== see fragment description ... */

package main

import (

var (
    port int

func homeHandler(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, "/redirectTo", http.StatusFound)
func targetHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "You have been redirected here!")
func init() {
    flag.IntVar(&port, "port", 8080, "HTTP Server Port")
func main() {
    httpAddr := fmt.Sprintf(":%v", port)
    log.Printf("Listening on port%v ...\n", httpAddr)
    http.HandleFunc("/redirectTo", targetHandler)
    http.HandleFunc("/", homeHandler)
    srv := http.Server{Addr: httpAddr}