Skip to content

Latest commit

Β 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Full Cycle Challenge: Multithreading in Go

Go Version Status

This project is the solution for the Multithreading challenge from the Full Cycle Postgraduate Program in Go. The main goal is to apply advanced concurrency concepts such as goroutines, channels, and the select statement to fetch data from multiple APIs simultaneously, accepting only the fastest response.

🧠 Architecture

The project consists of a CLI application that orchestrates a race between two distinct APIs:

  1. Simultaneous Requests:

    • The system triggers two parallel requests using goroutines to fetch address data from a specific ZIP code (CEP).
    • Target 1: BrasilAPI
    • Target 2: ViaCEP
  2. Race & Timeout Logic:

    • Using the select statement, the application waits for the first API to return a valid response.
    • A maximum timeout of 1 second is enforced. If no API responds within this limit, a timeout error is displayed.
    • Upon a winner, the slower request is discarded via context cancellation to save system resources.
  3. Data Normalization (DTOs):

    • Specific Data Transfer Objects (DTOs) are used to map different JSON structures from each API into a unified Address entity, ensuring architectural decoupling.

πŸ“ Project Structure

Following Go's best practices and the challenge requirements, all code is contained in a single repository on the main branch:

.
β”œβ”€β”€ main.go          # Core logic, concurrency handling, and DTO mapping
β”œβ”€β”€ go.mod           # Go module dependencies
└── README.md        # Documentation

πŸš€ How to Run

To test the application, provide a valid 8-digit Brazilian CEP as an argument.

  1. Execute the Application: Run the following command replacing <cep> with a real Brazilian zip code number (ex.:01153000):
go run main.go <cep>

Note: You can run this command multiple times. Depending on network latency, the winner API (BrasilAPI or ViaCEP) may change, demonstrating the race condition in real-time.


πŸ“Š Output Examples

Successful Race Winner:

Winner: BrasilAPI
Address: {CEP:01153000 State:SP City:SΓ£o Paulo Area:Barra Funda Street:Rua Vitorino Carmilo Source:BrasilAPI}

Timeout Reached (Global limit of 1s):

Error: Timeout reached (1s). Neither API responded in time.

About

Solution for the Multithreading and APIs challenge from Full Cycle's Go Postgraduate program

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages