什么时候应该返回值而不是修改接收者指针?

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
}

Both options you show might be useful sometimes. May I propose another possibility. In Go we should use functions much more often then in other languages. A plain function might be exactly what you are looking for:

// Run performs a proof-of-work
func Run(pow *ProofOfWork) (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[:]
}

I would probably make ProofOfWork an interface, and abstract Run that way.