0Pricing
Go Academy · Lesson

Creating an HTTP Server

ListenAndServe, HandlerFunc, and ServeMux

net/http package

Go's standard library net/http provides a production-capable HTTP/1.1 and HTTP/2 server without external dependencies.

Basic server

Register a handler with http.HandleFunc and start listening with http.ListenAndServe:

http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello, World!")
})
log.Fatal(http.ListenAndServe(":8080", nil))

All lessons in this course

  1. Creating an HTTP Server
  2. Routing and Path Parameters
  3. Middleware Pattern
  4. Graceful Shutdown
← Back to Go Academy