package main
import (
"fmt"
"net/http"
"io/ioutil"
"encoding/json"
)
const (
apiKey = "YOUR_API_KEY" // Replace with your Alpha Vantage API key
symbol = "GME" // Stock symbol for GameStop
)
type StockQuote struct {
GlobalQuote struct {
Symbol string `json:"01. symbol"`
Open float64 `json:"02. open,string"`
High float64 `json:"03. high,string"`
Low float64 `json:"04. low,string"`
Price float64 `json:"05. price,string"`
Volume int `json:"06. volume,string"`
LatestTradingDay string `json:"07. latest trading day"`
PreviousClose float64 `json:"08. previous close,string"`
Change float64 `json:"09. change,string"`
ChangePercent string `json:"10. change percent"`
} `json:"Global Quote"`
}
func main() {
url := fmt.Sprintf("https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=%s&apikey=%s", symbol, apiKey)
response, err := http.Get(url)
if err != nil {
fmt.Println("Error fetching stock data:", err)
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return
}
var stockQuote StockQuote
err = json.Unmarshal(body, &stockQuote)
if err != nil {
fmt.Println("Error decoding JSON:", err)
return
}
fmt.Printf("Symbol: %s\n", stockQuote.GlobalQuote.Symbol)
fmt.Printf("Open: $%.2f\n", stockQuote.GlobalQuote.Open)
fmt.Printf("High: $%.2f\n", stockQuote.GlobalQuote.High)
fmt.Printf("Low: $%.2f\n", stockQuote.GlobalQuote.Low)
fmt.Printf("Price: $%.2f\n", stockQuote.GlobalQuote.Price)
fmt.Printf("Volume: %d\n", stockQuote.GlobalQuote.Volume)
fmt.Printf("Latest Trading Day: %s\n", stockQuote.GlobalQuote.LatestTradingDay)
fmt.Printf("Previous Close: $%.2f\n", stockQuote.GlobalQuote.PreviousClose)
fmt.Printf("Change: $%.2f\n", stockQuote.GlobalQuote.Change)
fmt.Printf("Change Percent: %s\n", stockQuote.GlobalQuote.ChangePercent)
}