A Golang package to fetch stock prices from Google Finance, by using the =GOOGLEFINANCE() function in Google Sheets.
- Go to the Google Cloud Console
- Create a new project
- Enable Google Sheets API for your project
- Create a service account
- Follow Step 4 on that page to generate the service account and download the JSON key
- Save the key on the disk in JSON format
- Go to Google Drive and create a new Google Sheet
- Copy the email address of your service account
- Share the sheet with that email and assign the Editor role
- Get your Google Sheet ID - it's the string in the URL between
/d/and/edit
export GOOGLE_SHEETS_CREDENTIALS=/path/to/the/file/with/credentials
export GOOGLE_SHEETS_ID=YOUR_GOOGLE_SHEET_IDgo get github.com/levinishka/go-googlefinancepackage main
import (
"fmt"
"log"
"os"
finance "github.com/levinishka/go-googlefinance"
)
func main() {
config := &finance.Config{
CredentialsPath: os.Getenv("GOOGLE_SHEETS_CREDENTIALS"),
SpreadsheetId: os.Getenv("GOOGLE_SHEETS_ID"),
TtlInSec: 300,
BalancerNumberOfThreads: 3,
}
googleFinanceClient, err := finance.NewGoogleFinanceClient(config)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
res, err := googleFinanceClient.ReadPrices([]string{"VTI", "VGT", "GE", "T"})
if err != nil {
log.Fatalf("Failed to read prices: %v", err)
}
fmt.Println(res)
}