FAQ
Hi, all

I was trying to calculate a checksum of a single map via injecting the map
to the sha1 with gob encoding, the expected checksum results should be
unique, however it can be different because the keys order in a map is not
guaranteed.
So my question is, Is that my misusage or may be it's a bug of golang?


http://play.golang.org/p/gIVw6rklOj

package main

import (
"bytes"
"crypto/sha1"
"encoding/gob"
"fmt"
"io"
)

func main() {
v := map[string]string{"name": "Felix", "age": "18"}

cs := chechSum(&v)
cs2 := chechSum(&v)
for bytes.Compare(cs, cs2) == 0 {
cs2 = chechSum(&v)
}

fmt.Println(cs)
fmt.Println(cs2)
}

func chechSum(i interface{}) []byte {
sh1 := sha1.New()
io.WriteString(sh1, "q3244214")
ge := gob.NewEncoder(sh1)
ge.Encode(i)
return sh1.Sum(nil)
}

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

  • Jesse McNelis at Jul 8, 2014 at 1:59 am

    On Tue, Jul 8, 2014 at 11:54 AM, yeer wrote:
    So my question is, Is that my misusage or may be it's a bug of golang?
    It's not a bug, maps are unordered in Go.
    If you want to order then you need to fetch the values in the order
    you want them in.
    A common solution to this is to store the keys in a slice in the order
    you want them and then iterate over the slice and looking up each key
    in the map.

    --
    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.
  • Yeer at Jul 8, 2014 at 2:29 am
    I know the reason is the unordered map.
    It makes sense to two map with same key/values, but for the same original
    map, it seems odd. If you thinking from a high level way,
    take the map as an integrated single object, it produce different checksum.
    So maybe the implementation of the inner encoding logic should use ordered
    map, instead of let users do it in a dirty hacking way from the out side
    yard?

    It's at least a trap if it's not a bug.





    On Tuesday, July 8, 2014 9:59:58 AM UTC+8, Jesse McNelis wrote:

    On Tue, Jul 8, 2014 at 11:54 AM, yeer <yeer...@gmail.com <javascript:>>
    wrote:
    So my question is, Is that my misusage or may be it's a bug of golang?
    It's not a bug, maps are unordered in Go.
    If you want to order then you need to fetch the values in the order
    you want them in.
    A common solution to this is to store the keys in a slice in the order
    you want them and then iterate over the slice and looking up each key
    in the map.
    --
    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.
  • Jesse McNelis at Jul 8, 2014 at 3:27 am

    On Tue, Jul 8, 2014 at 12:29 PM, yeer wrote:
    It's at least a trap if it's not a bug.
    It's not a trap, it's well specified and well documented.
    It's rather common among languages to have maps be unordered.
    Python, Javascript, Ruby1.8

    It's a trap for them to be ordered under certain conditions and not others.
    eg. python's behaviour of maps being ordered between updates means
    that writers can break readers in unexpected ways which is the
    behaviour you're expecting from Go.

    --
    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.
  • Yeer at Jul 8, 2014 at 3:46 am
    Hi Jesse, thanks for the info.
    I am not arguing the correctness of unordered map,
    just thought in this certain scenario, use the unordered keys iteration may
    be wrong.

    https://code.google.com/p/go/source/browse/src/pkg/encoding/gob/encode.go#327




    On Tuesday, July 8, 2014 11:27:22 AM UTC+8, Jesse McNelis wrote:

    On Tue, Jul 8, 2014 at 12:29 PM, yeer <yeer...@gmail.com <javascript:>>
    wrote:
    It's at least a trap if it's not a bug.
    It's not a trap, it's well specified and well documented.
    It's rather common among languages to have maps be unordered.
    Python, Javascript, Ruby1.8

    It's a trap for them to be ordered under certain conditions and not
    others.
    eg. python's behaviour of maps being ordered between updates means
    that writers can break readers in unexpected ways which is the
    behaviour you're expecting from Go.
    --
    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.
  • Dave Cheney at Jul 8, 2014 at 2:27 pm
    I don't see the bug, this will be decoded into a map on the other side of
    the gob transaction.
    On Tuesday, 8 July 2014 13:46:30 UTC+10, yeer wrote:

    Hi Jesse, thanks for the info.
    I am not arguing the correctness of unordered map,
    just thought in this certain scenario, use the unordered keys iteration
    may be wrong.


    https://code.google.com/p/go/source/browse/src/pkg/encoding/gob/encode.go#327




    On Tuesday, July 8, 2014 11:27:22 AM UTC+8, Jesse McNelis wrote:
    On Tue, Jul 8, 2014 at 12:29 PM, yeer wrote:
    It's at least a trap if it's not a bug.
    It's not a trap, it's well specified and well documented.
    It's rather common among languages to have maps be unordered.
    Python, Javascript, Ruby1.8

    It's a trap for them to be ordered under certain conditions and not
    others.
    eg. python's behaviour of maps being ordered between updates means
    that writers can break readers in unexpected ways which is the
    behaviour you're expecting from Go.
    --
    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.
  • Milan P. Stanic at Jul 8, 2014 at 5:44 am

    On Tue, 2014-07-08 at 13:27, Jesse McNelis wrote:
    On Tue, Jul 8, 2014 at 12:29 PM, yeer wrote:
    It's at least a trap if it's not a bug.
    It's not a trap, it's well specified and well documented.
    It's rather common among languages to have maps be unordered.
    Python, Javascript, Ruby1.8 Perl too.
    It's a trap for them to be ordered under certain conditions and not others.
    eg. python's behaviour of maps being ordered between updates means
    that writers can break readers in unexpected ways which is the
    behaviour you're expecting from Go.
    --
    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.
  • Rui Ueyama at Jul 8, 2014 at 2:31 am
    This is a very good read about Go map which answers your question.

    http://blog.golang.org/go-maps-in-action#TOC_7.

    On Mon, Jul 7, 2014 at 6:54 PM, yeer wrote:

    Hi, all

    I was trying to calculate a checksum of a single map via injecting the map
    to the sha1 with gob encoding, the expected checksum results should be
    unique, however it can be different because the keys order in a map is not
    guaranteed.
    So my question is, Is that my misusage or may be it's a bug of golang?


    http://play.golang.org/p/gIVw6rklOj

    package main

    import (
    "bytes"
    "crypto/sha1"
    "encoding/gob"
    "fmt"
    "io"
    )

    func main() {
    v := map[string]string{"name": "Felix", "age": "18"}

    cs := chechSum(&v)
    cs2 := chechSum(&v)
    for bytes.Compare(cs, cs2) == 0 {
    cs2 = chechSum(&v)
    }

    fmt.Println(cs)
    fmt.Println(cs2)
    }

    func chechSum(i interface{}) []byte {
    sh1 := sha1.New()
    io.WriteString(sh1, "q3244214")
    ge := gob.NewEncoder(sh1)
    ge.Encode(i)
    return sh1.Sum(nil)
    }

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 8, '14 at 1:54a
activeJul 8, '14 at 2:27p
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase