Last active
March 21, 2019 01:45
-
-
Save suhanlee/9c2e64dc3f3fb35cf0c80f6b89503244 to your computer and use it in GitHub Desktop.
http
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 process(w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
fmt.Fprintln(w, r.Form) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/process", process) | |
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 process(w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
fmt.Fprintln(w, r.PostForm) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/process", process) | |
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 process(w http.ResponseWriter, r *http.Request) { | |
r.ParseMultipartForm(1024) | |
fmt.Fprintln(w, r.MultipartForm) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/process", process) | |
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 process(w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
fmt.Fprintln(w, r.FormValue("hello")) | |
fmt.Fprintln(w, r.PostFormValue("hello")) | |
fmt.Fprintln(w, r.Form) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/process", process) | |
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 main() { | |
server := http.Server { | |
Addr: "127.0.0.1:8080", | |
Handler: nil, | |
} | |
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 body(w http.ResponseWriter, r *http.Request) { | |
len := r.ContentLength | |
body := make([]byte, len) | |
r.Body.Read(body) | |
fmt.Fprintln(w, string(body)) | |
} | |
func main() { | |
server := http.Server { | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/body", body) | |
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" | |
) | |
type MyHandler struct{} | |
func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello, World!") | |
} | |
func main() { | |
handler := MyHandler{} | |
server := http.Server { | |
Addr: "127.0.0.1:8080", | |
Handler: &handler, | |
} | |
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" | |
) | |
type HelloHandler struct{} | |
func (h *HelloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello") | |
} | |
type WorldHandler struct{} | |
func (h *WorldHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "World!") | |
} | |
func main() { | |
hello := HelloHandler{} | |
world := WorldHandler{} | |
server := http.Server { | |
Addr: "127.0.0.1:8080", | |
} | |
http.Handle("/hello", &hello) | |
http.Handle("/world", &world) | |
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 hello(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello!") | |
} | |
func world(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "World!") | |
} | |
func main() { | |
server := http.Server { | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/hello", hello) | |
http.HandleFunc("/world", world) | |
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" | |
"runtime" | |
"reflect" | |
) | |
func hello(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello!") | |
} | |
func log(h http.HandlerFunc) http.HandlerFunc { | |
return func(w http.ResponseWriter, r *http.Request) { | |
name := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name() | |
fmt.Printf("Handler function called - " + name) | |
h(w, r) | |
} | |
} | |
func main() { | |
server := http.Server { | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/hello", log(hello)) | |
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" | |
) | |
type HelloHandler struct{} | |
func (h HelloHandler) ServeHTTP (w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello!") | |
} | |
func log(h http.Handler) http.Handler { | |
return http.HandlerFunc (func(w http.ResponseWriter, r *http.Request) { | |
fmt.Printf("Handler called -%T\n", h) | |
h.ServeHTTP(w, r) | |
}) | |
} | |
func protect(h http.Handler) http.Handler { | |
return http.HandlerFunc (func(w http.ResponseWriter, r *http.Request) { | |
h.ServeHTTP(w, r) | |
}) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
hello := HelloHandler{} | |
http.Handle("/hello", protect(log(hello))) | |
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" | |
"github.com/julienschmidt/httprouter" | |
"fmt" | |
) | |
func hello(w http.ResponseWriter, r *http.Request, p httprouter.Params){ | |
fmt.Fprintf(w, "hello, %s!\n", p.ByName("name")) | |
} | |
func main() { | |
mux := httprouter.New() | |
mux.GET("/hello/:name", hello) | |
server := http.Server { | |
Addr: "127.0.0.1:8080", | |
Handler: mux, | |
} | |
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" | |
"golang.org/x/net/http2" | |
) | |
type MyHandler struct{} | |
func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello World!") | |
} | |
func main() { | |
handler := MyHandler{} | |
server := http.Server { | |
Addr: "127.0.0.1:8080", | |
Handler: &handler, | |
} | |
http2.ConfigureServer(&server, &http2.Server{}) | |
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 headers(w http.ResponseWriter, r *http.Request) { | |
h := r.Header | |
fmt.Fprintln(w, h) | |
} | |
func main() { | |
server := http.Server{ | |
Addr: "127.0.0.1:8080", | |
} | |
http.HandleFunc("/headers", headers) | |
server.ListenAndServe() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment