https://github.com/kplachkov/btcprice
Btcprice is a fast, simple and clean way to get the price of Bitcoin. The package uses the Blockchain API.
https://github.com/kplachkov/btcprice
bitcoin bitcoin-price blockchain blockchain-api btc golang golang-library price
Last synced: 5 months ago
JSON representation
Btcprice is a fast, simple and clean way to get the price of Bitcoin. The package uses the Blockchain API.
- Host: GitHub
- URL: https://github.com/kplachkov/btcprice
- Owner: kplachkov
- License: bsd-3-clause
- Created: 2018-04-03T19:56:30.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-04T10:44:55.000Z (almost 8 years ago)
- Last Synced: 2024-06-20T13:46:16.586Z (almost 2 years ago)
- Topics: bitcoin, bitcoin-price, blockchain, blockchain-api, btc, golang, golang-library, price
- Language: Go
- Homepage:
- Size: 125 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://codecov.io/gh/kplachkov/btcprice)
[](https://travis-ci.org/kplachkov/btcprice)
[](https://goreportcard.com/report/github.com/kplachkov/btcprice)
[](https://godoc.org/github.com/kplachkov/btcprice)
[](https://github.com/kplachkov/btcprice/releases)
**Btcprice** is a fast, simple and clean way to get the price of Bitcoin. The package uses the Blockchain API as a source of the price. Btcprice can be used with custom source of data too.
* [Installation](#installation)
* [Getting Started](#getting-started)
* [License](#license)
## Installation
### Install via `go get`
To install btcprice, use `go get`.
```bash
$ go get github.com/kplachkov/btcprice/
```
## Getting Started
```bash
$ godoc github.com/kplachkov/btcprice/
```
### Code Example:
```go
package main
import (
"fmt"
"log"
"time"
"github.com/kplachkov/btcprice"
)
// Prints the time and the price of Bitcoin.
func printBtcPriceAndTime(btcPrice float64, t time.Time) {
fmt.Println(t.Format(time.RFC3339))
fmt.Println("Bitcoin price:", btcPrice)
}
// Checks for changes in the price of Bitcoin.
func checkBtcPrice(timeDuration time.Duration) {
blockchain, serErr := btcprice.NewBlockchainService()
if serErr != nil {
log.Fatal(serErr)
}
btcPrice := blockchain.BitcoinPrice.Usd.Last // The latest price of Bitcoin.
printBtcPriceAndTime(btcPrice, time.Now())
// Prints the time and the latest Bitcoin price if the price has changed.
for t := range time.NewTicker(timeDuration).C {
newBtcErr := blockchain.Update(nil)
if newBtcErr != nil {
log.Fatal(newBtcErr)
}
newBtcPrice := blockchain.BitcoinPrice.Usd.Last
// If the price has changed, it prints the time and up to date price.
if btcPrice != newBtcPrice {
printBtcPriceAndTime(newBtcPrice, t)
}
btcPrice = newBtcPrice
}
}
func main() {
checkBtcPrice(time.Second) // Check the price of Bitcoin every second.
}
```
## License
BSD 3-Clause
Copyright (c) 2018-Present, kplachkov