Structuring a Go CLI that fetches a URL
WHAT IT TESTS: basic Go CLI, HTTP, and error handling. OUTLINE: parse args with the flag package, http.Get the URL, check err and status, defer resp.Body.Close, copy body to stdout, exit non-zero on failure.
WHAT IT TESTS: idiomatic structure of a small Go program with flags, HTTP, and explicit error handling. ANSWER OUTLINE: define flags with the flag package and call flag.Parse, read the URL argument, call http.Get, check the returned error, defer resp.Body.Close to avoid leaks, optionally check resp.StatusCode, stream the body to os.Stdout with io.Copy, and on any error print to stderr and os.Exit non-zero. RED FLAG: discarding errors with blank identifiers or never closing the body, leaking connections.
Read the original → interview
- #go
- #cli
- #http
- #error-handling
- #stdlib
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.