### Get Gold Price with Go Source: https://github.com/joel-g/go-goldapi/blob/master/README.md Demonstrates how to initialize the API client and fetch the price of silver for a given currency and date. Ensure your API key is set as an environment variable 'gold_api_key'. ```go package main import ( "fmt" "log" "os" "github.com/joel-g/go-goldapi/goldapi" ) func main() { // Get your API key from https://www.goldapi.io api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) // Use one of the 4 const Metals from goldapi package: // Must include currency in ISO 4217. // Date is optional but must be in YYYYMMDD format. // If date is left blank today's date will be used. silverReport, err := api.GetPrice(goldapi.Silver, "USD", "") if err != nil { log.Fatal(err) } fmt.Printf("The price of silver is %f\n", silverReport.Price) fmt.Printf("%+v", silverReport) // A report of your API usage stats, err := api.GetStats() if err != nil { log.Fatal(err) } fmt.Printf("\nYou have used %d requests this month", stats.RequestsMonth) } ``` -------------------------------- ### Get Metal Price and Print Report Source: https://context7.com/joel-g/go-goldapi/llms.txt Fetches the current spot price for a specified metal and currency, then prints all fields from the MetalReport struct. Requires the 'gold_api_key' environment variable to be set. ```go // MetalReport field reference type MetalReport struct { Timestamp int // Unix timestamp of the price data Metal string // ISO metal code (e.g., "XAU") Currency string // ISO currency code (e.g., "USD") Exchange string // Exchange source (e.g., "FOREXCOM") Symbol string // Trading symbol (e.g., "FOREXCOM:XAGUSD") PrevClosePrice float64 // Previous day's closing price OpenPrice float64 // Opening price for the current session LowPrice float64 // Session low HighPrice float64 // Session high OpenTime int // Unix timestamp of session open Price float64 // Current spot price Ch float64 // Absolute price change from previous close Chp float64 // Percentage price change from previous close Ask float64 // Ask price Bid float64 // Bid price } // Example: print all fields from a GetPrice response package main import ( "fmt" "log" "os" "time" "github.com/joel-g/go-goldapi/goldapi" ) func main() { api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) r, err := api.GetPrice(goldapi.Palladium, "USD", "") if err != nil { log.Fatal(err) } fmt.Printf("Metal: %s / %s\n", r.Metal, r.Currency) fmt.Printf("Exchange: %s (%s)\n", r.Exchange, r.Symbol) fmt.Printf("Time: %s\n", time.Unix(int64(r.Timestamp), 0).UTC()) fmt.Printf("Price: $%.4f\n", r.Price) fmt.Printf("Open: $%.4f\n", r.OpenPrice) fmt.Printf("High: $%.4f\n", r.HighPrice) fmt.Printf("Low: $%.4f\n", r.LowPrice) fmt.Printf("Prev Close: $%.4f\n", r.PrevClosePrice) fmt.Printf("Change: %.4f (%.2f%%)\n", r.Ch, r.Chp) fmt.Printf("Bid/Ask: $%.4f / $%.4f\n", r.Bid, r.Ask) } ``` -------------------------------- ### Fetch Metal Prices with go-goldapi Source: https://context7.com/joel-g/go-goldapi/llms.txt Fetch current or historical spot prices for precious metals. Use an empty string for the date to get the current price, or a YYYYMMDD formatted string for historical data. Ensure the metal and currency codes are valid. ```go package main import ( "fmt" "log" "os" "github.com/joel-g/go-goldapi/goldapi" ) func main() { api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) // --- Current price of Gold in USD --- goldReport, err := api.GetPrice(goldapi.Gold, "USD", "") if err != nil { log.Fatalf("GetPrice error: %v", err) } fmt.Printf("Gold price (USD): %.4f\n", goldReport.Price) fmt.Printf("Bid: %.4f Ask: %.4f Change: %.4f (%.2f%%)\n", goldReport.Bid, goldReport.Ask, goldReport.Ch, goldReport.Chp) // --- Historical price of Silver in EUR on 2023-06-15 --- silverHistory, err := api.GetPrice(goldapi.Silver, "EUR", "20230615") if err != nil { log.Fatalf("GetPrice error: %v", err) } fmt.Printf("Silver on 2023-06-15 (EUR): %.4f\n", silverHistory.Price) // --- Platinum in GBP (current) --- ptReport, err := api.GetPrice(goldapi.Platinum, "GBP", "") if err != nil { log.Fatalf("GetPrice error: %v", err) } fmt.Printf("Platinum price (GBP): %.4f High: %.4f Low: %.4f\n", ptReport.Price, ptReport.HighPrice, ptReport.LowPrice) // --- Gold/Silver ratio via XAG currency code --- ratioReport, err := api.GetPrice(goldapi.Gold, "XAG", "") if err != nil { log.Fatalf("GetPrice error: %v", err) } fmt.Printf("Gold/Silver ratio: %.4f\n", ratioReport.Price) // Sample full MetalReport output: // {Timestamp:1604535350 Metal:XAG Currency:USD Exchange:FOREXCOM // Symbol:FOREXCOM:XAGUSD PrevClosePrice:23.895 OpenPrice:23.895 // LowPrice:23.895 HighPrice:24.042 OpenTime:1604527200 Price:24.04 // Ch:0.145 Chp:0.61 Ask:24.059 Bid:24.011} } ``` -------------------------------- ### Initialize Goldapi API Client Source: https://context7.com/joel-g/go-goldapi/llms.txt Create a new API client using your Goldapi.io API key. The API key should be provided via an environment variable. ```go package main import ( "fmt" "os" "github.com/joel-g/go-goldapi/goldapi" ) func main() { // Initialize the client using an API key from an environment variable api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) fmt.Printf("Client ready: %+v\n", api) // Output: Client ready: &{Key:abc123 Host:https://www.goldapi.io/api/ Client:0xc0000b4000} } ``` -------------------------------- ### goldapi.NewAPIClient Source: https://context7.com/joel-g/go-goldapi/llms.txt Initializes and returns an *API client configured with the provided Goldapi.io API key and the base URL. All subsequent requests are made through this client. ```APIDOC ## goldapi.NewAPIClient — Create an API client Initializes and returns an `*API` client configured with the provided Goldapi.io API key and the base URL `https://www.goldapi.io/api/`. All subsequent requests are made through this client using a standard `net/http.Client`. ```go package main import ( "fmt" "os" "github.com/joel-g/go-goldapi/goldapi" ) func main() { // Initialize the client using an API key from an environment variable api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) fmt.Printf("Client ready: %+v\n", api) // Output: Client ready: &{Key:abc123 Host:https://www.goldapi.io/api/ Client:0xc0000b4000} } ``` ``` -------------------------------- ### Fetch API Usage Statistics Source: https://context7.com/joel-g/go-goldapi/llms.txt Retrieves API usage statistics for the current day, yesterday, current month, and previous month. This is useful for monitoring quota consumption against plan limits. Requires the 'gold_api_key' environment variable to be set. ```go package main import ( "fmt" "log" "os" "github.com/joel-g/go-goldapi/goldapi" ) func main() { api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) stats, err := api.GetStats() if err != nil { log.Fatalf("GetStats error: %v", err) } fmt.Printf("Requests today: %d\n", stats.RequestsToday) fmt.Printf("Requests yesterday: %d\n", stats.RequestsYesterday) fmt.Printf("Requests this month: %d\n", stats.RequestsMonth) fmt.Printf("Requests last month: %d\n", stats.RequestsLastMonth) // Sample output: // Requests today: 12 // Requests yesterday: 45 // Requests this month: 312 // Requests last month: 891 } ``` -------------------------------- ### api.GetPrice Source: https://context7.com/joel-g/go-goldapi/llms.txt Queries the Goldapi.io API for the spot price of a precious metal in a given currency. The `metal` argument must be one of the four package-level constants. The `currency` must be a 3-character ISO 4217 code. The `date` parameter is optional. ```APIDOC ## api.GetPrice — Fetch current or historical metal prices Queries the Goldapi.io API for the spot price of a precious metal in a given currency. The `metal` argument must be one of the four package-level constants (`goldapi.Gold`, `goldapi.Silver`, `goldapi.Platinum`, `goldapi.Palladium`). The `currency` must be a 3-character ISO 4217 code. The `date` parameter is optional — pass an empty string for today's price, or a `YYYYMMDD`-formatted string for a historical price. Returns a `*MetalReport` or an error if the API responds with a non-200 status or error body. **Supported metal constants:** | Constant | ISO Code | Metal | |--------------------|----------|-----------| | `goldapi.Gold` | `XAU` | Gold | | `goldapi.Silver` | `XAG` | Silver | | `goldapi.Platinum` | `XPT` | Platinum | | `goldapi.Palladium`| `XPD` | Palladium | **Supported currency codes:** `USD`, `AUD`, `GBP`, `EUR`, `CHF`, `CAD`, `JPY`, `INR`, `SGD`, `BTC`, `CZK`, `RUB`, `PLN`, `MYR`, `XAG` (Gold/Silver ratio). ```go package main import ( "fmt" "log" "os" "github.com/joel-g/go-goldapi/goldapi" ) func main() { api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) // --- Current price of Gold in USD --- goldReport, err := api.GetPrice(goldapi.Gold, "USD", "") if err != nil { log.Fatalf("GetPrice error: %v", err) } fmt.Printf("Gold price (USD): %.4f\n", goldReport.Price) fmt.Printf("Bid: %.4f Ask: %.4f Change: %.4f (%.2f%%)\n", goldReport.Bid, goldReport.Ask, goldReport.Ch, goldReport.Chp) // --- Historical price of Silver in EUR on 2023-06-15 --- silverHistory, err := api.GetPrice(goldapi.Silver, "EUR", "20230615") if err != nil { log.Fatalf("GetPrice error: %v", err) } fmt.Printf("Silver on 2023-06-15 (EUR): %.4f\n", silverHistory.Price) // --- Platinum in GBP (current) --- ptReport, err := api.GetPrice(goldapi.Platinum, "GBP", "") if err != nil { log.Fatalf("GetPrice error: %v", err) } fmt.Printf("Platinum price (GBP): %.4f High: %.4f Low: %.4f\n", ptReport.Price, ptReport.HighPrice, ptReport.LowPrice) // --- Gold/Silver ratio via XAG currency code --- ratioReport, err := api.GetPrice(goldapi.Gold, "XAG", "") if err != nil { log.Fatalf("GetPrice error: %v", err) } fmt.Printf("Gold/Silver ratio: %.4f\n", ratioReport.Price) // Sample full MetalReport output: // {Timestamp:1604535350 Metal:XAG Currency:USD Exchange:FOREXCOM // Symbol:FOREXCOM:XAGUSD PrevClosePrice:23.895 OpenPrice:23.895 // LowPrice:23.895 HighPrice:24.042 OpenTime:1604527200 Price:24.04 // Ch:0.145 Chp:0.61 Ask:24.059 Bid:24.011} } ``` ``` -------------------------------- ### GetStats - Fetch API Usage Statistics Source: https://context7.com/joel-g/go-goldapi/llms.txt Retrieves API usage statistics, including request counts for the current day, yesterday, current month, and previous month. This is useful for monitoring quota consumption. ```APIDOC ## GetStats ### Description Fetches API usage statistics, providing counts of requests made today, yesterday, this month, and last month. Helps in monitoring quota usage. ### Method Signature `func (c *APIClient) GetStats() (*StatsReport, error)` ### Parameters None ### Response #### Success Response (*StatsReport) - **RequestsToday** (int) - Number of requests made today. - **RequestsYesterday** (int) - Number of requests made yesterday. - **RequestsMonth** (int) - Number of requests made in the current month. - **RequestsLastMonth** (int) - Number of requests made in the previous month. ### Request Example ```go package main import ( "fmt" "log" "os" "github.com/joel-g/go-goldapi/goldapi" ) func main() { api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) stats, err := api.GetStats() if err != nil { log.Fatalf("GetStats error: %v", err) } fmt.Printf("Requests today: %d\n", stats.RequestsToday) fmt.Printf("Requests yesterday: %d\n", stats.RequestsYesterday) fmt.Printf("Requests this month: %d\n", stats.RequestsMonth) fmt.Printf("Requests last month: %d\n", stats.RequestsLastMonth) // Sample output: // Requests today: 12 // Requests yesterday: 45 // Requests this month: 312 // Requests last month: 891 } ``` ``` -------------------------------- ### MetalReport Structure Source: https://github.com/joel-g/go-goldapi/blob/master/README.md This is a sample JSON structure for a MetalReport returned by the API, showing fields like Timestamp, Metal, Currency, and Price. ```json {Timestamp:1604535350 Metal:XAG Currency:USD Exchange:FOREXCOM Symbol:FOREXCOM:XAGUSD PrevClosePrice:23.895 OpenPrice:23.895 LowPrice:23.895 HighPrice:24.042 OpenTime:1604527200 Price:24.04 Ch:0.145 Chp:0.61 Ask:24.059 Bid:24.011} ``` -------------------------------- ### GetPrice - Fetch Spot Price Source: https://context7.com/joel-g/go-goldapi/llms.txt Retrieves the current spot price for a specified metal and currency pair. The response includes detailed market data such as timestamp, exchange information, OHLC values, bid/ask spread, and price changes. ```APIDOC ## GetPrice ### Description Fetches the current spot price for a given metal and currency. Returns a `MetalReport` struct containing comprehensive market data. ### Method Signature `func (c *APIClient) GetPrice(metal string, currency string, exchange string) (*MetalReport, error)` ### Parameters - **metal** (string) - Required - ISO metal code (e.g., "XAU", "XAG"). - **currency** (string) - Required - ISO currency code (e.g., "USD", "EUR"). - **exchange** (string) - Optional - Specific exchange to query (e.g., "COMEX"). If empty, uses default. ### Response #### Success Response (*MetalReport) - **Timestamp** (int) - Unix timestamp of the price data. - **Metal** (string) - ISO metal code. - **Currency** (string) - ISO currency code. - **Exchange** (string) - Exchange source. - **Symbol** (string) - Trading symbol. - **PrevClosePrice** (float64) - Previous day's closing price. - **OpenPrice** (float64) - Opening price for the current session. - **LowPrice** (float64) - Session low price. - **HighPrice** (float64) - Session high price. - **OpenTime** (int) - Unix timestamp of session open. - **Price** (float64) - Current spot price. - **Ch** (float64) - Absolute price change from previous close. - **Chp** (float64) - Percentage price change from previous close. - **Ask** (float64) - Ask price. - **Bid** (float64) - Bid price. ### Request Example ```go package main import ( "fmt" "log" "os" "time" "github.com/joel-g/go-goldapi/goldapi" ) func main() { api := goldapi.NewAPIClient(os.Getenv("gold_api_key")) r, err := api.GetPrice(goldapi.Palladium, "USD", "") if err != nil { log.Fatal(err) } fmt.Printf("Metal: %s / %s\n", r.Metal, r.Currency) fmt.Printf("Exchange: %s (%s)\n", r.Exchange, r.Symbol) fmt.Printf("Time: %s\n", time.Unix(int64(r.Timestamp), 0).UTC()) fmt.Printf("Price: $%.4f\n", r.Price) fmt.Printf("Open: $%.4f\n", r.OpenPrice) fmt.Printf("High: $%.4f\n", r.HighPrice) fmt.Printf("Low: $%.4f\n", r.LowPrice) fmt.Printf("Prev Close: $%.4f\n", r.PrevClosePrice) fmt.Printf("Change: %.4f (%.2f%%)\n", r.Ch, r.Chp) fmt.Printf("Bid/Ask: $%.4f / $%.4f\n", r.Bid, r.Ask) } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.