如何在Go中生成字符串的哈希数? [英] How to generate hash number of a string in Go?
本文介绍了如何在Go中生成字符串的哈希数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
hash("HelloWorld") = 1234567
是否有任何内置功能可以做到这一点?
Is there any built-in function could do this ?
谢谢.
推荐答案
哈希软件包很有帮助为了这.请注意,这是对特定哈希实现的抽象.在软件包子目录中可以找到一些现成的东西.
The hash package is helpful for this. Note it's an abstraction over specific hash implementations. Some ready made are found in the package subdirectories.
示例:
package main
import (
"fmt"
"hash/fnv"
)
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func main() {
fmt.Println(hash("HelloWorld"))
fmt.Println(hash("HelloWorld."))
}
(也此处)
输出:
926844193
107706013
这篇关于如何在Go中生成字符串的哈希数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文