Skip to content

Instantly share code, notes, and snippets.

@suhanlee
Created September 21, 2017 17:52
Show Gist options
  • Save suhanlee/db69fa2c22433ebe677c2b7bf4c1b00b to your computer and use it in GitHub Desktop.
Save suhanlee/db69fa2c22433ebe677c2b7bf4c1b00b to your computer and use it in GitHub Desktop.
http_redirect.go
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()
}
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()
}
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