在Go语言中,如何创建一个map[[16]byte][]string呢?

huangapple go评论88阅读模式
英文:

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

问题

根据Go规范说明

>比较运算符==和!=(§比较运算符)必须对键类型的操作数进行完全定义;因此,键类型不能是结构体、数组或切片。如果键类型是接口类型,则这些比较运算符必须对动态键值进行定义;否则会导致运行时恐慌。

我希望创建一个哈希值的映射,这些哈希值来自于Hash接口,该接口返回[]byte,但是我知道所有的哈希都是用相同的算法完成的(因此我知道它适合[16]byte)。我该如何提供适当的接口,以便map类型允许使用[]byte[16]byte或其包装类型作为键?

目前我的使用会生成以下错误:

dupes := make(map[[16]byte][]string)

<pre>finddups.go:55: invalid map key type [16]uint8</pre>


更新(2012年3月): Go1允许[16]byte作为键类型。

英文:

The Go spec states:

>The comparison operators == and != (§Comparison operators) must be fully defined for operands of the key type; thus the key type must not be a struct, array or slice. If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time panic.

I wish to create a map of hash values which come from the Hash interface, which returns []byte, but for which all my hashes are done with the same algorithm (thereby I know that it would fit into [16]byte). How can I provide the appropriate interface such that the map type will allow []byte or [16]byte or some wrapper thereof to be used as a key?

Presently my use generates the following error:

dupes := make(map[[16]byte][]string)

<pre>finddups.go:55: invalid map key type [16]uint8</pre>


Update (Mar 2012): Go1 allows [16]byte as key type.

答案1

得分: 4

一个string类型代表了一组UTF-8编码的字符串值。字符串的行为类似于字节的数组。有关字节切片的规则2和4,请参阅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语言和许多其他语言中都可以使用。

英文:

A string type represents the set of UTF-8 encoded string values. Strings behave like arrays of bytes. See rules 2 and 4 for byte slices in the Conversions to and from a string type topic in the Conversions section of The Go Language Specification.

package main

import &quot;fmt&quot;

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{&quot;a&quot;, &quot;b&quot;}
	hash[len(hash)-1]++
	dupes[string(hash)] = []string{&quot;b&quot;, &quot;c&quot;}

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

Output:

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]

Note: This algorithm merely takes advantage of the rules for string types and conversions given in The Go Language Specification, which all implementations must satisfy. It's a "trick", just like var i int = &#39;7&#39; - &#39;0&#39; (for ASCII, EBCDIC, Unicode, etc.) is a "trick" to convert a numeric character '7' to an integer value 7, in Go and many other languages.

huangapple
  • 本文由 发表于 2010年11月26日 23:41:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/4286615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定