为了账号安全,请及时绑定邮箱和手机立即绑定

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

// 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 回答

fmt.Printf("\r%x", hash)

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

0 回复 有任何疑惑可以回复我~

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

otoevx

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信