func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, world!") } func main() { http.HandleFunc("/", helloHandler) http.ListenAndServe(":8000", nil) }
func redirectHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "http://example.com", http.StatusFound) } func main() { http.HandleFunc("/redirect", redirectHandler) http.ListenAndServe(":8000", nil) }In this example, we define a function `redirectHandler` that redirects the user to "http://example.com" with a HTTP status of 302 (Temporary Redirect). We register this handler for the "/redirect" path, and start the server listening on port 8000 as before. Both of these examples use the net/http package library in Go. The `http.HandleFunc` and `http.ListenAndServe` functions are part of this package, as well as `http.Redirect`.