Skip to content

Chapter 14: The Basic Server

Goal: Make the computer listen and talk back. Concept: net/http is the standard library for web servers.

go
package main

import (
	"fmt"
	"net/http"
)

// Lesson 1: The Server
// Goal: Listen for connections and talk back.

func main() {
	// 1. The Handler (The Clerk)
	// When someone visits "/", run this function.
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello! Welcome to The Gopher Shop (v1)")
	})

	// 2. The Listener (The Open Sign)
	// Listen on port 8080.
	fmt.Println("🚀 Shop is open at http://localhost:8080")
	http.ListenAndServe(":8080", nil)
}
🎓 Knowledge Check: What does http.ListenAndServe do?

Answer: It starts the server and keeps it running (listening) in a loop. Without it, your program would exit immediately!

Released under the MIT License.