Add lesson 2: Your first HTTP server

Daniel Tanaka 1 year ago
parent 061016b601 commit b43c939df3555be094e289e8d1ea8e2912d8864d
3 changed files with 37 additions and 0 deletions
+19-0lessons/02-your-first-http-server/README.md ADDED
@@ -0,0 +1,19 @@
1+# Lesson 2: Your first HTTP server
2+
3+Your first HTTP server comes up in almost every real Go project. We start with the why, then write code together.
4+
5+Full lesson: [https://glibcode.net/courses/go-apis/lessons/2](https://glibcode.net/courses/go-apis/lessons/2)
6+
7+## Files
8+
9+- `main.go`: a hello endpoint with net/http
10+
11+## Run it
12+
13+```sh
14+go run ./lessons/02-your-first-http-server
15+```
16+
17+## Exercises
18+
19+See [exercises.md](exercises.md). Ask in the forum once you've tried.
+5-0lessons/02-your-first-http-server/exercises.md ADDED
@@ -0,0 +1,5 @@
1+# Exercises: lesson 2
2+
3+1. Combine this lesson with the previous one: use something from both in a small program of your own.
4+2. Add a comment above each block explaining what it does, in your own words.
5+3. Time yourself: rewrite the example from memory in 10 minutes, then diff it against the original.
+13-0lessons/02-your-first-http-server/main.go ADDED
@@ -0,0 +1,13 @@
1+package main
2+
3+import (
4+ "fmt"
5+ "net/http"
6+)
7+
8+func main() {
9+ http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
10+ fmt.Fprintf(w, "Hello, %s!\n", r.URL.Query().Get("name"))
11+ })
12+ http.ListenAndServe(":8080", nil)
13+}