Created
September 21, 2017 17:52
-
-
Save suhanlee/db69fa2c22433ebe677c2b7bf4c1b00b to your computer and use it in GitHub Desktop.
http_redirect.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"fmt" | |
) | |
func writeExample(w http.ResponseWriter, r *http.Request) { | |
str := `<html> | |
<head><title>hello</title></head> | |
<body> hello world! </body> | |
</html> | |
` | |
w.Write([]byte(str)) | |
} | |
func writeHeaderExample(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(501) | |
fmt.Fprintln(w, "No such service, try agin.") | |
} | |
func redirectExample(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Location", "http://google.com") | |
w.WriteHeader(302) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/write", writeExample) | |
http.HandleFunc("/writeheader", writeHeaderExample) | |
http.HandleFunc("/redirect", redirectExample) | |
server.ListenAndServe() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "net/http" | |
func writeExample(w http.ResponseWriter, r *http.Request) { | |
str := `<html> | |
<head><title>hello</title></head> | |
<body> hello world! </body> | |
</html> | |
` | |
w.Write([]byte(str)) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/write", writeExample) | |
server.ListenAndServe() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"fmt" | |
) | |
func writeExample(w http.ResponseWriter, r *http.Request) { | |
str := `<html> | |
<head><title>hello</title></head> | |
<body> hello world! </body> | |
</html> | |
` | |
w.Write([]byte(str)) | |
} | |
func writeHeaderExample(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(501) | |
fmt.Fprintln(w, "No such service, try agin.") | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/write", writeExample) | |
http.HandleFunc("/writeheader", writeHeaderExample) | |
server.ListenAndServe() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment