I have a method for the struct ProofOfWork which should modify the struct members Nonce and Hash. So I wonder whether it should modify these two members of the given instance inside the method Run or should make these two variables as a return.
So here is the method Run with return variables:
// 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\"
", pow.block.Data)
for nonce < maxNonce {
data := pow.prepareData(nonce)
hash = sha256.Sum256(data)
fmt.Printf("%x", hash)
hashInt.SetBytes(hash[:])
if hashInt.Cmp(pow.target) == -1 {
break
} else {
nonce++
}
}
fmt.Print("
")
return nonce, hash[:]
}
Then the version without any return variables:
func (pow *ProofOfWork) Run() {
var hashInt big.Int
var hash [32]byte // the type of hash value is defined by result of the sha256 function
nonce := 0
for nonce < MaxNonce {
data := pow.prepareData(nonce)
hash := sha256.Sum256(data)
hashInt.SetBytes(hash[:])
if hashInt.Cmp(pow.target) == -1 {
// the nonce found
break
} else {
nonce++
}
}
pow.block.Hash = hash[:]
pow.block.Nonce = nonce
}