fmt.Printf的输出为什么是覆盖滚动显示的而不是逐句输出全部显示?

来源:4-1 工作量证明及哈希算法

iepngs

2019-12-17 17:49

// run performs a proof-of-work
func (pow *ProofOfWork) Run() (int, []byte) {
   var hashInt big.Int
   var hash [32]byte
   nonce := 0

   fmt.Printf("mining the block containing \"%s\"\n", pow.block.Data)
   for nonce < maxNonce {
      data := pow.prepareData(nonce)

      hash = sha256.Sum256(data)
      fmt.Printf("\r%x", hash)
      hashInt.SetBytes(hash[:])

      if hashInt.Cmp(pow.target) == -1 {
         break
      } else {
         nonce++
      }
   }
   fmt.Print("\n\n")

   return nonce, hash[:]
}

这里的run方法,for循环里面的fmt.Printf的输出为什么是覆盖滚动显示的而不是逐句输出全部显示?

写回答 关注

2回答

  • 慕标5272691
    2020-02-03 21:31:23
    已采纳
    fmt.Printf("\r%x", hash)

    \r 代表回车,每行打印完后,下一次光标会移至改行的行首,重新覆盖打印;\n 是换行,这里换成\n就是全部显示

  • 慕仰1157001
    2022-07-19 21:12:30

    这个在DW编辑—首选项—代码提示中是可以设置的。

    otoevx

私有区块链,我们一起GO

用Go语言实现一个区块链私有链

23742 学习 · 48 问题

查看课程

相似问题