在Go语言中查找一个地址的BTC余额。

huangapple go评论119阅读模式
英文:

Find BTC balance for an address in Go

问题

我需要纠正和改造一个函数,用于检查两个比特币地址的余额,比如(addr 和 addr1),并返回 JSON 值。我需要说明一下,我对 Golang 不是很了解,需要帮助!

  1. func balance(addr) {
  2. var url = "https://bitcoin.toshi.io/api/v0/addresses/" + addr
  3. response, _ := http.Get(url)
  4. defer response.Body.Close()
  5. contents, _ := ioutil.ReadAll(response.Body)
  6. return contents
  7. }

编辑:

我需要一个函数来获取两个比特币地址的余额,或者使用这些库 btcwallet

  1. func GetAddressBalance(icmd btcjson.Cmd) (interface{}, *btcjson.Error)
  2. func GetAddressesByAccount(icmd btcjson.Cmd) (interface{}, *btcjson.Error)
  3. func GetBalance(icmd btcjson.Cmd) (interface{}, *btcjson.Error)
英文:

I need to correct and transform a function, to checking 2 addresses of btc balance, like (addr and addr1 ), and the return is json value, i need to specify im not know very well golang.. need help!

  1. func balance(addr) {
  2. var url = "https://bitcoin.toshi.io/api/v0/addresses/" + addr
  3. response, _ := http.Get(url)
  4. defer response.Body.Close()
  5. contents, _ := ioutil.ReadAll(response.Body)
  6. return contents
  7. }

EDIT:

i need a function to get balances of 2 btc addresses, or to use this libraries btcwallet.

  1. func GetAddressBalance(icmd btcjson.Cmd) (interface{}, *btcjson.Error)
  2. func GetAddressesByAccount(icmd btcjson.Cmd) (interface{}, *btcjson.Error)
  3. func GetBalance(icmd btcjson.Cmd) (interface{}, *btcjson.Error)

答案1

得分: 0

你走在正确的道路上。API返回一个JSON。可以将其解组为一个结构体(Address)。这样你就可以访问API返回的所有内容。不要忘记始终检查错误代码!将some_address替换为实际的地址以获取余额。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. func main() {
  8. a, err := getAddress("some_address")
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println("Balance:", a.Balance)
  14. }
  15. type Address struct {
  16. Hash string
  17. Balance int
  18. Received int
  19. Send int
  20. UnconfirmedBalance int `json:"unconfirmed_balance"`
  21. UnconfirmedReceived int `json:"unconfirmed_received"`
  22. UnconfirmedSent int `json:"unconfirmed_sent"`
  23. }
  24. func getAddress(addr string) (*Address, error) {
  25. resp, err := http.Get("https://bitcoin.toshi.io/api/v0/addresses/" + addr)
  26. if err != nil {
  27. return nil, err
  28. }
  29. defer resp.Body.Close()
  30. if resp.StatusCode != http.StatusOK {
  31. return nil, fmt.Errorf("Error: %v", http.StatusText(resp.StatusCode))
  32. }
  33. var address Address
  34. decoder := json.NewDecoder(resp.Body)
  35. if err := decoder.Decode(&address); err != nil {
  36. return nil, err
  37. }
  38. return &address, nil
  39. }
英文:

You were on the right track. The api returns a json. This can be unmarshalled into a struct (Address). That way you can access everything returned by the api. Do not forget to always check for error codes! Replace some_address with an actual address to get the balance.

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. func main() {
  8. a, err := getAddress("some_address")
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Println("Balance:", a.Balance)
  14. }
  15. type Address struct {
  16. Hash string
  17. Balance int
  18. Received int
  19. Send int
  20. UnconfirmedBalance int `json:"unconfirmed_balance"`
  21. UnconfirmedReceived int `json:"unconfirmed_received"`
  22. UnconfirmedSent int `json:"unconfirmed_sent"`
  23. }
  24. func getAddress(addr string) (*Address, error) {
  25. resp, err := http.Get("https://bitcoin.toshi.io/api/v0/addresses/" + addr)
  26. if err != nil {
  27. return nil, err
  28. }
  29. defer resp.Body.Close()
  30. if resp.StatusCode != http.StatusOK {
  31. return nil, fmt.Errorf("Error: %v", http.StatusText(resp.StatusCode))
  32. }
  33. var address Address
  34. decoder := json.NewDecoder(resp.Body)
  35. if err := decoder.Decode(&address); err != nil {
  36. return nil, err
  37. }
  38. return &address, nil
  39. }

huangapple
  • 本文由 发表于 2016年2月22日 18:48:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/35551634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定