Socket
Book a DemoInstallSign in
Socket

github.com/gustavo-villar/go-multithreading-api-calls

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/gustavo-villar/go-multithreading-api-calls

v0.0.0-20240810115403-9576d3246e8f
Source
Go
Version published
Created
Source

Address Fetcher Service

This project is a Go application that retrieves address information based on a provided Brazilian postal code (CEP) by querying two different APIs concurrently. The program returns the address data from the API that responds the fastest, ensuring a quick and efficient result.

How it Works

The application sends concurrent requests to the following APIs:

  • BrasilAPI
  • ViaCEP

It uses the API that provides the first response, ignoring the slower one. The program also includes a timeout mechanism to ensure the entire process completes within 1 second. If neither API responds within the time limit, a timeout message is displayed.

Requirements

  • Go 1.16 or later.

How to Run the Application

  • Clone the repository or save the code to a file named main.go
  • Ensure you have Go installed on your system.
  • Run the application:
go run main.go [CEP]

Replace [CEP] with the desired Brazilian postal code. If no CEP is provided, the application defaults to 01153000.

Output

The application prints the fetched address to the command line with the following format:

Endereço (API): Logradouro, Bairro, Localidade - UF

If the request exceeds the 1-second timeout or an error occurs, the program will print a corresponding error message.

Example

go run main.go 01001000

Expected output:

// BrasilAPI
Endereço (Brasil API): Praça da Sé, Sé, São Paulo - SP

or

// ViaCEP
Endereço (ViaCEP): Praça da Sé, Sé, São Paulo - SP

If a timeout occurs:

// ViaCEP
Timeout: Nenhuma das APIs respondeu em tempo hábil.

Aditional Information

  • Timeout: The application enforces a 1-second timeout to ensure responsiveness.
  • APIs: The application chooses the first responding API to minimize the wait time for the user.

FAQs

Package last updated on 10 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.