Skip to content

Module 1: HTTP Standard Library (The Postal Service)

📚 Module 1: net/http

Course ID: GO-201
Subject: The Postal Service

Go is unique because you don’t need a framework to build a high-performance web server. Everything is included in the standard library.


🏗️ Step 1: The Request-Response Loop

🧩 The Analogy: The Postal Service

  1. Request: A user writes a letter (The Request) and sends it to an address (The URL).
  2. Server: Your Go program acts as the Postal Clerk. It opens the letter and reads what the user wants.
  3. Response: You write a reply (The Response) and send it back.

🏗️ Step 2: In Code

func helloHandler(w http.ResponseWriter, r *http.Request) {
    // 1. Write the message back
    fmt.Fprintf(w, "Hello, Welcome to the Post Office!")
}

func main() {
    // 2. Map the address to the function
    http.HandleFunc("/hello", helloHandler)

    // 3. Start the server on port 8080
    http.ListenAndServe(":8080", nil)
}

🏗️ Step 3: Why is it so fast?

Because Go’s net/http server automatically starts a Goroutine (Module 3) for every single user that visits your site. It can handle tens of thousands of people at the same time without slowing down.


🥅 Module 1 Review

  1. http.HandleFunc: Mapping a URL to a function.
  2. http.ResponseWriter: Your “Pen” for writing the response.
  3. http.Request: The “Incoming Letter” with user data.

:::tip Slow Learner Note You don’t need to install anything! Just run the code above and visit localhost:8080/hello in your browser. :::