How to create a map [[16] byte] [] string in Go?

The Go specification indicates :

Comparison operators == and! = (§ Matching operators) must be fully defined for key type operands; therefore, the key type should not be a structure, array, or slice. If the key type is an interface type, these comparison operators must be defined for dynamic key values; failure will lead to panic at runtime.

I want to create a map of hash values ​​that come from the Hash interface , which returns []byte, but for which all my hashes are executed using the same algorithm (so I know that it will fit in [16]byte). How can I provide an appropriate interface so that the type mapallows you to use []byteeither [16]byteor some kind of wrapper to use as a key?

My use is currently causing the following error:

dupes := make(map[[16]byte][]string)
finddups.go: 55: invalid map key type [16] uint8

Update (March 2012): Go1 resolves [16]byteas a key type.

+5
source share
1 answer

A UTF-8. . . 2 4 "" Conversions Go.

package main

import "fmt"

func main() {
    dupes := make(map[string][]string)

    hash := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    dupes[string(hash)] = []string{"a", "b"}
    hash[len(hash)-1]++
    dupes[string(hash)] = []string{"b", "c"}

    fmt.Println("len:", len(dupes))
    for k, v := range dupes {
        fmt.Println("key:", []byte(k), "value:", v)
    }
}

:

len: 2
key: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16] value: [b c]
key: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] value: [a b]

. , Go, . "", var i int = '7' - '0' ( ASCII, EBCDIC, Unicode ..) "" "7" 7, Go .

+3

All Articles