dongmi3203 2018-07-15 17:08
浏览 27
已采纳

类型转换错误

I am trying to follow the tutorial here : https://goethereumbook.org/block-query/

In his code , he calls the header and then hard codes it into blockNumber.

 header, err := client.HeaderByNumber(context.Background(), nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(header.Number.String()) // 5671744

    blockNumber := big.NewInt(5671744)

I have attempted to improve on this and converted the string to int64.

header, err := client.HeaderByNumber(context.Background(), nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(header.Number.String())

    var stringBlockNumber = header.Number.String()

    int64BlockNumber, err := strconv.ParseInt(stringBlockNumber, 10, 64)
    if err != nil {
        log.Fatal(err)
    }

    blockNumber := big.NewInt(int64BlockNumber)

    block, err := client.BlockByNumber(context.Background(), blockNumber)
    if err != nil {
        log.Fatal(err)
    }

This works until in try to add the following line:

fmt.Println(block.Number.Uint64())

I get the following error in the terminal :

./queryBlock.go:41:26: block.Number.Uint64 undefined (type func() *big.Int has no field or method Uint64)

I would appreciate any pointers on this.

  • 写回答

1条回答 默认 最新

  • dongzhuo5185 2018-07-15 17:20
    关注

    After getting the block by number through client.BlockByNumber, block is of type Block from /github.com/ethereum/go-ethereum/core/types.

    Number of Block is a method, which returns a *big.Int instead of being a field of that type. So To retrieve the number, you should call the method, i.e, block.Number().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)