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")
}
200which is a "success" status code called "OK". Redirections are done with3xxstatus codes, like 301, 302, 307... List of common redirection codes pre-declared in Go can be found here: pkg.go.dev/net/[email protected]#StatusIMUsed. And you can read more on the topic here: developer.mozilla.org/en-US/docs/Web/HTTP/Redirections and more on statuses: developer.mozilla.org/en-US/docs/Web/HTTP/Status303for that case: evertpot.com/http/303-see-other . 307 is definitely not correct.