duanji1043 2018-01-06 04:57
浏览 23

从函数返回数据

I am trying to return data from a function. In this example, I am looking up a bitcoin address on Blockchain.info, and what I would like to do is call the function, passing it the address, and return the final balance in a variable that I can use. Can someone show me what I am doing wrong?

package main

import (
  "fmt"
   "io/ioutil"
   "net/http"
    "os"
)

func getbalance(address string) {
  response, err := http.Get("https://blockchain.info/q/addressbalance/" + address)
  if err != nil {
      fmt.Printf("%s", err)
      os.Exit(1)
  } else {
      defer response.Body.Close()
      contents, err := ioutil.ReadAll(response.Body)
      if err != nil {
          fmt.Printf("%s", err)
          os.Exit(1)
      }
      fmt.Printf("%s
", string(contents))
      return string(contents)
  }
}


func main() {
  finalB := getbalance("1A63imbiQBtsnTqtUpuUT5WL2Ti7oKNLeg")
  fmt.Printf("Final Balance: %s", finalB)
}
  • 写回答

1条回答 默认 最新

  • duanfengshang1088 2018-01-06 05:12
    关注

    Your function returns nothing:

    func getbalance(address string) {
       ....
    }
    

    That function signature, return string:

    func getbalance(address string) string {
      ...
    }
    

    I suggest you to learn basic Go functions

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类