A Go application that fetches real-time cryptocurrency exchange rates using the CEX.io API. The application supports concurrent requests for multiple cryptocurrencies.
.
├── main.go # Entry point with concurrent goroutines
├── go.mod # Go module definition
├── Api/
│ ├── cex.go # CEX.io API client
│ ├── responses.go # API response structures
│ └── Cex_test.go # Unit tests
├── datatypes/
│ └── data.go # Data type definitions
└── hello/
- Concurrent API Calls: Uses Go goroutines to fetch multiple cryptocurrency prices simultaneously
- CEX.io Integration: Fetches real-time exchange rates from the CEX.io API
- Error Handling: Validates input and handles API errors gracefully
- Clean Data Types: Structured types for API responses and rate data
- Bitcoin (BTC)
- Ethereum (ETH)
- Bitcoin Cash (BCH)
- Go 1.22.2 or higher
git clone https://github.com/ayushman1210/Crypto_tracker.git
cd crypto
go mod tidygo run .This will fetch the current USD exchange rates for BTC, ETH, and BCH concurrently.
go test ./...- URL:
https://cex.io/api/ticker/{CURRENCY}/USD - Method: GET
- Response: JSON with bid/ask prices and volume data
- Currency code must be exactly 3 characters long
- Automatically converts input to uppercase
Uses sync.WaitGroup to manage concurrent API requests:
var wg sync.WaitGroup
for _, currency := range currencies {
wg.Add(1)
go func(currencycode string) {
getcurrency(currencycode)
wg.Done()
}(currency)
}
wg.Wait()Getcurr(currency string): Fetches the current price for a given cryptocurrency- Handles HTTP requests, JSON parsing, and error validation
- Returns
*datatypes.Ratecontaining currency and price information
type Rate struct {
Currency string
Price float64
}The application handles various error scenarios:
- Invalid currency codes (not 3 characters)
- Network errors
- HTTP status code errors
- JSON parsing errors
the rate for BTC is 45000.00
the rate for ETH is 2500.50
the rate for BCH is 350.75
- Standard Go libraries only:
net/httpencoding/jsonsynciofmtstrings
Ayushman1210