FAQ
to implement the hash.Hash interface you also need Write and Reset which would not work with a value receiver. all the interface methods need to be implemented on either a value or a pointer. you cannot mix them.

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?
On Tuesday, July 8, 2014 1:01:50 PM UTC-4, Chris Kastorff wrote:
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 interface
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.

Search Discussions

  • Chris Kastorff at Jul 9, 2014 at 1:34 am

    Would marking the method as a value receiver (rather than a pointer
    receiver as it currently is) not do the same thing?
    Yes.
    to implement the hash.Hash interface you also need Write and Reset
    which would not work with a value receiver. all the interface methods
    need to be implemented on either a value or a pointer. you cannot mix
    them.
    Incorrect. http://play.golang.org/p/jG9vEOvxUs

    Of course, the value type does not implement the interface, but that's
    to be expected.



    Thinking on when I use value methods, I think it's just for clarity;
    mixing value recievers and pointer recievers creates a bit of confusion
    when most methods mutate the value. The * in the declaration is really
    easy to miss; it's clearer if everything is the same.

    --
    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.
  • Akwillis at Jul 9, 2014 at 2:51 pm
    This is valid: http://play.golang.org/p/zDt6LxHDlT
    This is valid: http://play.golang.org/p/weuozddq3r
    This is valid: http://play.golang.org/p/282YC5RXxw
    This is not valid: http://play.golang.org/p/GM7E24w4nL
    This is not valid: http://play.golang.org/p/aPHACbAisZ

    The rule about pointers vs. values for receivers is that value methods can
    be invoked on pointers and values, but pointer methods can only be invoked
    on pointers
    On Tuesday, July 8, 2014 9:35:01 PM UTC-4, Chris Kastorff wrote:

    Would marking the method as a value receiver (rather than a pointer
    receiver as it currently is) not do the same thing?
    Yes.
    to implement the hash.Hash interface you also need Write and Reset
    which would not work with a value receiver. all the interface methods
    need to be implemented on either a value or a pointer. you cannot mix
    them.
    Incorrect. http://play.golang.org/p/jG9vEOvxUs

    Of course, the value type does not implement the interface, but that's
    to be expected.
    I don't think you absorbed the full context of the quoted text. You kind of
    just read that last line.

    Can't mix receiver types with values and pointers and expect it to
    implement an interface, all methods satisfying an interface must implement
    the same method set including the same receiver. And in the case of your
    example, it is just one of those quirky exceptions and compiler magic saves
    the day.

    http://golang.org/doc/effective_go.html#pointers_vs_values

    --
    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.
  • Martin Schnabel at Jul 10, 2014 at 12:07 am

    On 07/09/2014 04:50 PM, akwillis wrote:
    This is valid: http://play.golang.org/p/zDt6LxHDlT
    This is valid: http://play.golang.org/p/weuozddq3r
    This is valid: http://play.golang.org/p/282YC5RXxw
    This is not valid: http://play.golang.org/p/GM7E24w4nL
    This is not valid: http://play.golang.org/p/aPHACbAisZ

    The rule about pointers vs. values for receivers is that value methods
    can be invoked on pointers and values, but pointer methods can only be
    invoked on pointers
    thank you both.

    this is a bit embarrassing. i read the spec often enough but somehow did
    not connect the Method Sets with the Interface types section.

    and sorry chris for not checking the spec before sending out the mail.
    but in my defense it was from my phone.


    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 8, '14 at 11:58p
activeJul 10, '14 at 12:07a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase