duanjiao5543 2012-11-27 10:46
浏览 914
已采纳

如何在Go中生成字符串的哈希数?

For example:

hash("HelloWorld") = 1234567

Is there any built-in function could do this ?

Thanks.

  • 写回答

2条回答 默认 最新

  • duan35557593 2012-11-27 11:09
    关注

    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.

    Example:

    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."))
    }
    

    (Also here)


    Output:

    926844193
    107706013
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app