I mainly work with PHP, learning Go is something I do as a hobby. I really like teaching and showing others how things work, hopefully my guide here will do a decent job of explaining how to track the price of STEEM. When I explain things I will often compare it to how PHP works as that is what I am used to working with.
Let's get started. First thing is you will need to install go, you can do this on linux, mac or pc. Here is what I am using for this tutorial, everything except Go is optional.
- Go v1.8.3
- Gogland
- A macbook pro
- CryptoCompare API (You can probably follow along with any API with small adjustments)
So let's get started. I am using the CryptoCompare API which claims to be the best API around and lists several websites that use their API, all they ask is that you link to their site if you use their API. It allows for up to 4000 requests every hour but we should be ok with a request every minute for our own personal use, every 10 seconds to see that it's working.
I want to make use of their price method that takes 2 arguments, the currency you want to check and then a list of currencies you would like to see the price for the first currency in.
Lets navigate to our $GOPATH folder and create a new project. I created my project under /go/src/github.com/markustenghamn/golang-steem-cryptotracker and then added this folder as a new project in Gogland. I then created a file called price.go. The initial file looks like this:
package main
func main() {
}
So let's begin by declaring our variables. I want to know the value of 1 STEEM if I were to convert it to USD, thus a from and a to variable along with our url where I have added the variables. We will use this url to make our request and get our json data.
package main
func main() {
From := "STEEM"
To := "USD"
url := "https://min-api.cryptocompare.com/data/price?fsym=" + From + "&tsyms=" + To
}
Now, let's make our request.
package main
import (
"net/http"
"time"
"log"
}
func main() {
From := "STEEM"
To := "USD"
url := "https://min-api.cryptocompare.com/data/price?fsym=" + From + "&tsyms=" + To
// We create a http client with a timeout of 30 seconds, you can probably set this lower if you would like
w := http.Client{
Timeout: time.Second * 30,
}
// We create a new request, I uses _ to ignore any error here
req, _ := http.NewRequest(http.MethodGet, url, nil)
// We execute the request
res, getErr := w.Do(req)
if getErr != nil {
log.Println(getErr)
} else {
// We will put the rest of our code here. This runs if the request is successful
}
}
You may also have noticed that I added a few import statements. This is because we need http, log and time to setup the request, make the request and handle any errors. We will be adding a few more import statements before we are done. Right now we make the request but we also need to handle the data. I left a comment in the code where the next part will go. You can also find a link to the entire sample at the bottom of this article. So let's handle the data, we will parse the json, create a type to hold the data and then output the data. We could create a struct for the json data but this api is fairly simple so all we need is the type as a map (an array basically).
// We read the response body
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Println(readErr)
} else {
// If the response can be read we move on the parsing the json
r := Result{}
// We unmarshal the json using our result type
jsonErr := json.Unmarshal(body, &r)
if jsonErr != nil {
log.Println(jsonErr)
} else {
// We loop through all of our results and echo them, should only be one result
for _, p := range r {
fmt.Println("1 "+From+" = "+To)
}
}
}
I added a result type right below the import statement.
type Result map[string]float64
My import statement now looks like this:
import (
"net/http"
"time"
"io/ioutil"
"encoding/json"
"fmt"
"log"
)
Now if you run this you should get an output like this 1 STEEM = 1.270000 USD
. Now, running the script every time we want to check the price is not optimal. Therefore we want to put all the code we just wrote into a function that we can call every 10 seconds for example so that we get the most recent price. My new function is called ´getPrice´ and takes a time parameter so we can output this in our console. My main function has been changed and looks like this:
func main() {
doEvery(time.Second * 10, getPrice)
}
My doEvery function looks like this:
func doEvery(d time.Duration, f func(time.Time)) {
for x := range time.Tick(d) {
f(x)
}
}
I also modified the output string so that it now looks like this:
fmt.Printf("%v: 1 "+From+" = "+strconv.FormatFloat(p, 'f', 6, 64)+" "+To+"\n", t)
That's it, now you should have a stream of data pouring in every 10 seconds. I plan to keep developing this app, make it a bit more useful. I would like to track my STEEM, Bitcoin and any other altcoin. It would be nice to see a statistics on when I earned or bought my coin and how the price has changed over time so that I know if I have lost or gained money. I think it might be a cool project to launch as a website if others find it interesting.
You can also build this as an application with 'go build' and the application will run on linux, mac or windows without the need to install golang.
I have uploaded the complete sample to GitHub if anyone wants to take a look at the code: https://github.com/markustenghamn/golang-steem-cryptotracker
Wow!
Thanks for shedding some light on GO, please post more stuff like this.
I enjoy new twists and turns like you have displayed.
Followed, Upvoted and Resteemed
Thank you! Really appreciate the feedback, my plan is to keep making new posts in the future!
happy day