如何输入一个字符串,得到一个唯一的hashcode?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

import (
"fmt"
"hash/crc32"
)

// String hashes a string to a unique hashcode.
//
// crc32 returns a uint32, but for our use we need
// and non negative integer. Here we cast to an integer
// and invert it if the result is negative.
func String(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}


func main(){
str1 := "hello world"
str2 := "xmge.top"

for _, str := range []string{str1, str2} {
for i := 0; i < 3; i++ {
hc := String(str)
fmt.Println("hashcode:", hc)
}
}
}