Skip to content

Weekly Review — Month 2 · Week 2 (Days 036–042)

Journal index · Roadmap › Month 2 Week 2

📅 The Week in One Line

Assembled the standard library into a service-building toolkit: encoding/json (incl. struct tags & custom marshalers), the time package, the net/http client, context deadlines/cancellation, and log/slog structured logging.

✅ What I Completed

  • Day 036 — encoding/json Marshal & Unmarshal (+ json-tags example)
  • Day 037 — struct tags & custom Marshaler/Unmarshaler (+ tempjson exercise)
  • Day 038 — time: durations, formatting, timers (+ time-timers example, humandur exercise)
  • Day 039 — net/http client & requests (+ http-context example)
  • Day 040 — context timeouts & cancellation (+ retry exercise)
  • Day 041 — log/slog structured logging (+ slog-structured example)
  • Day 042 — Review & closed-book recall
  • Runnable examples: 4 (json-tags, time-timers, http-context, slog-structured)
  • Exercises solved: 3 (tempjson, humandur, retry) — all with table-driven tests

💡 Lessons Learned

  • json only sees exported fields; Unmarshal needs a pointer; numbers into interface{} become float64.
  • Custom JSON: MarshalJSON can use a value receiver, but UnmarshalJSON must be a pointer receiver or it's silently ignored. Beware self-recursion inside MarshalJSON.
  • time.Duration is nanoseconds; format/parse with the magic reference date 2006-01-02 15:04:05; always Stop() a Ticker.
  • http.Get/DefaultClient have no timeout; always close resp.Body; a 404/500 is data, not a Go error.
  • A context only signals — blocking code must select on ctx.Done(); always defer cancel(); use errors.Is for DeadlineExceeded/Canceled.
  • slog: handler decides format/destination/level; prefer typed attrs over loose pairs; With builds request-scoped child loggers.

💪 Strengths (what clicked)

  • Struct tags and the Marshaler/Unmarshaler escape hatch — the custom Celsius round trip felt natural.
  • The context + http pairing: per-call WithTimeout bound to a request via NewRequestWithContext.
  • slog typed attributes and child loggers.

🧩 Weaknesses (what's still fuzzy)

  • json.RawMessage and partial/lazy decoding.
  • time.Timer.Reset correctness (draining the channel) and Round vs Truncate edge cases.
  • Tuning http.Transport (connection pool, dial timeouts) beyond Client.Timeout.
  • Writing a custom slog.Handler that injects a trace ID from context.

🔁 Spaced-Repetition Re-quiz (topics from earlier weeks)

  1. Q: (Month 1) nil slice vs nil map — which is safe to use, which panics?
    AA nil slice is safe to append to and range; writing to a nil map panics (reads return the zero value).
  2. Q: (Month 2 W1) Why must you Flush a bufio.Writer?
    AIt buffers writes in memory; without Flush the pending bytes never reach the underlying writer.
  3. Q: (Month 2 W1) Which fmt verb prints a value's Go-syntax form ignoring String()?
    A%#v.
  4. Q: (This week) Why must UnmarshalJSON have a pointer receiver?
    AIt must mutate the destination; json only invokes the Unmarshaler it finds on the addressable value, so a value receiver is never called and the field stays zero.
  5. Q: (This week) Does a ctx timeout stop a running time.Sleep?
    ANo — context only signals via Done(). Blocking work must select on ctx.Done() or use a context-aware API; otherwise it runs to completion.

🎯 Action Items

  • Refactor http-context fetch to decode JSON straight from the body with json.NewDecoder.
  • Write a tiny custom slog.Handler that pulls a request ID out of context.Context.
  • Re-read the time reference-layout table once more; drill three custom layouts from memory.
  • Add a json.RawMessage mini-experiment for deferred decoding.

🚀 Next Week Goals

  • Deep dive on testing: table-driven subtests with t.Run, testing.B benchmarks + b.ReportAllocs(), fuzzing, and coverage.
  • The go toolchain end to end: go test/vet/build, gofmt/goimports, modules & versioning.
  • Apply this week's pieces in a small CLI/service that fetches JSON over HTTP with a context timeout and structured logs.

📊 Metrics

Hours Days hit Exercises Commits Avg confidence
10.5 7/7 3 7 3.⅗

Suggested commit: docs(journal): month 2 week 2 review