How to Get the Current Stock Price of a Company with Go (Golang)
Learn how to make a simple command line program with Go.

Introduction
I’ve just started learning Go and I put together a simple module called finnhub-go that lets you query the Finnhub Stock API for the current price of a company.
In this post, I’ll show you how to create a simple command line program that takes the company ticker symbol as an argument and outputs its stock price.
Before we begin
Make sure you have Go installed on your local machine. In the following example, I’m using version 1.14.4 on a MacBook Pro.
Step 1: Create a new project
First of all, create a directory to contain your code.
We’ll call it stonks :-)
mkdir stonks
Change into the directory and run the go mod init
command to create a go.mod file:
cd stonks
go mod init
Step 2: Add the finnhub-go module
finnhub-go is a small module I wrote to query the Finnhub Stocks API. The API can be used for free without an account, but you’ll be limited number to a small number of requests per day. For more requests you can create a free account and use an API token when creating the client in the code which you’ll see in the next step.
Add the finnhub-go module to the project by running the following command:
go get github.com/tonymackay/finnhub-go
We’re now ready to create the main.go file and add some code.
Step 3: Create main.go
Create a new file called main.go and add the following code:
The code above creates a new client using an empty string for the token. You’ll want to sign up for a Finnhub account and change this to your token, if you plan on making lots of API calls. Alternatively, you can use another module I created that lets you get the stock price from Yahoo Finance instead.
Once the client is created, the next line takes the first argument passed into the program and uses it to call the Quote
function. The result of the Quote
function is then written to the console.
Let’s test the code to see it in action.
Test the program
Run the following command to run the program:
go run *.go TSLA
The command above will output the stock price for Tesla:
Symbol: TSLA
Close: 387.790009
Open: 393.470001
High: 408.732300
Low: 391.299988
Price: 405.102295
You can change the symbol argument passed to the program above to get the price of another company. For example, go run *.go AMZN
will get the stock price of Amazon.
Conclusion
This was a short demo of a module I’ve been working on that lets you get the stock prices of a company using the Finnhub Stock API. You can also use it to get a list of company symbols for a given exchange. For example, call client.StockSymbols(“US”)
to get a list of companies on the NYSE and NASDAQ.
View the GitHub repository.