Kevin Malachowski <niftastical@gmail.com> schrieb:
Would marking the method as a value receiver (rather than a pointer
receiver as it currently is) not do the same thing?
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--receiver as it currently is) not do the same thing?
On Tuesday, July 8, 2014 1:01:50 PM UTC-4, Chris Kastorff wrote:
properly, which states:
// Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
Sum(b []byte) []byte
Since checkSum modifies the state of the sha256.digest struct, it must
be done on a copy to avoid changing the original hash state and to allow
callers to continue writing to the old object (as is stated in the
comment of sha256.digest.Sum).
-Chris K
--On 07/08/2014 08:52 AM, Kevin Malachowski wrote:
I have a question regarding a pattern I noticed in crypto/sha256 and
crypto/sha512 (and maybe more of the stdlib, I didn't look further). In
the case of sha256, the digest.Sum (near here in the source
<http://golang.org/src/pkg/crypto/sha256/sha256.go#L114>) has a pointer
receiver but the first thing it does is dereference the pointer and use
that value for the rest of the method. It's even weirder in sha512
<http://golang.org/src/pkg/crypto/sha512/sha512.go#L115>.
Why the method was not just marked as a value receiver? A guess is that
it's a manual optimization* but I don't recall this sort of thing coming
up in the threads I've read here.
It's not an optimization, it's implementing the hash.Hash interfaceI have a question regarding a pattern I noticed in crypto/sha256 and
crypto/sha512 (and maybe more of the stdlib, I didn't look further). In
the case of sha256, the digest.Sum (near here in the source
<http://golang.org/src/pkg/crypto/sha256/sha256.go#L114>) has a pointer
receiver but the first thing it does is dereference the pointer and use
that value for the rest of the method. It's even weirder in sha512
<http://golang.org/src/pkg/crypto/sha512/sha512.go#L115>.
Why the method was not just marked as a value receiver? A guess is that
it's a manual optimization* but I don't recall this sort of thing coming
up in the threads I've read here.
properly, which states:
// Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
Sum(b []byte) []byte
Since checkSum modifies the state of the sha256.digest struct, it must
be done on a copy to avoid changing the original hash state and to allow
callers to continue writing to the old object (as is stated in the
comment of sha256.digest.Sum).
-Chris K
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.