1

With this code it show me a link to click for redirect to /hello, how do i make it redirect without user interaction?

package main
import (
    "fmt"
    "net/http"
)

func main() {
    r := http.NewServeMux()

    r.HandleFunc("/", index)
    r.HandleFunc("/hello", hello)

    http.ListenAndServe(":80", r)
}

func index(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, "/hello", 200)
}
func hello(w http.ResponseWriter, r *http.Request) {
    fmt.Println("hello")
}
4

1 Answer 1

3

As already answered in the comments, the standard HTTP code to be returned in case of a redirect is 303. So the redirect line should be:

http.Redirect(w, r, "/hello", http.StatusSeeOther)

Also note, using http package http.XXX is preferred to writing HTTP codes directly

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.