Golang - HTTP
HTTP Server
There are 2 parts of an HTTP server in go: handlers and server
http.HandleFunc: the logic of the handlershttp.ListenAndServe: start the server and listen to new HTTP requests.
Basic example
package main
import (
"fmt"
"io"
"net/http"
)
func handleRoot(w http.ResponseWriter, r *http.Request) {
fmt.Println("Handling request: /")
io.WriteString(w, "Home page\n")
}
func handleFoo(w http.ResponseWriter, r *http.Request) {
fmt.Println("Handling request: /foo")
io.WriteString(w, "Foo page\n")
}
func main() {
http.HandleFunc("/", handleRoot)
http.HandleFunc("/foo", handleFoo)
http.ListenAndServe(":8888", nil)
}
The server will run and block; open another shell as the client and try:
$ curl localhost:8888
Home page
$ curl localhost:8888/foo
Foo page
Note:
-
inside the
HandleFunc,fmt.Println()prints to the server, whileio.WriteStringwrites to the response so will show in the client shell. -
http.HandleFunc()usesDefaultServeMuxunder the hood; you can create your ownServeMux; aServerMuxis basically a set ofmuxEntry(pairs of string patterns like/fooandHandler). The example above is equivalent tomux := http.NewServeMux() mux.HandleFunc("/", handleRoot) mux.HandleFunc("/foo", handleFoo) http.ListenAndServe(":8888", mux) -
http.ListenAndServe()creates ahttp.Serverimplicitly (only setsAddrandHandler); alternatively you can create your ownhttp.Server;http.ListenAndServe()is equivalent to:server := &http.Server{ Addr: ":8888", Handler: nil, // ... } server.ListenAndServe()