Error handling and output management in Go can be done by building a client interacting with a weather server. The client fetches weather data from the server, handles errors, and outputs messages to standard output (stdout) and standard error (stderr).
The project consists of two main components:
- Client: Fetches weather data from the server, implements retry logic for rate-limited responses (HTTP 429), and properly reports errors.
- Server: Provided for testing, the server simulates various responses (successful weather data, HTTP 429 with a
Retry-After
header, and dropped connections).
- Open a terminal and navigate to the
server
folder:cd server
- Run the server:
The server listens on port 8080 and simulates various responses (normal, 429, dropped connections).
go run main.go
- Open another terminal and navigate to the project root:
cd output-and-error-handling
- Run the client:
The client fetches weather data from http://localhost:8080, handles retries for 429 responses, and prints output to stdout (weather data) or stderr (error messages).
go run main.go
To run the unit tests for the fetcher package:
go test ./fetcher