Skip to content
Learn Netverks

Lesson

Step 29/36 81% through track

http-server-go

HTTP server basics

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
advanced

This lesson

This lesson teaches HTTP server basics: the syntax, patterns, and safety habits you need before advancing in Go.

net/http is enough for many services—middleware patterns mirror Node and ASP.NET concepts.

You will apply HTTP server basics in contexts like: REST microservices, webhooks, and internal APIs behind API gateways.

Write Go in main.go with package main and func main(), click Run on server—the dev runner runs go run main.go; use fmt.Println for output (requires Go toolchain; LEARNING_RUNNER_ENABLED=true).

When pointers, structs, and basic control flow from intermediate lessons are familiar.

The net/http package provides production-grade HTTP clients and servers—no framework required for simple APIs. Compare with Express in Node.js or Spring in Java.

Handler pattern

http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "ok")
})
http.ListenAndServe(":8080", nil)

Handlers receive ResponseWriter and *Request. Use http.Server with timeouts in production—not the default zero timeouts.

Playground note

Long-running servers need local setup—this lesson prints handler concepts; run go run . locally to test with curl.

Important interview questions and answers

  1. Q: Default ServeMux?
    A: http.DefaultServeMux—prefer explicit mux in production to avoid accidental route registration.
  2. Q: Context in handlers?
    A: Use r.Context() for cancellation tied to client disconnect.

Self-check

  1. Which function registers a route on DefaultServeMux?
  2. Why set Server ReadTimeout?

Tip: Set ReadHeaderTimeout and ReadTimeout on http.Server—defaults allow slowloris-style resource exhaustion.

Interview prep

Default ServeMux risks?

Global registration—prefer explicit mux in production to avoid accidental routes.

Handler context?

Use r.Context() for cancellation when client disconnects.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ListenAndServe?
  • Handler interface?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump