用Go编码UTF-8 [英] Encoding UTF-8 with Go

查看:136
本文介绍了用Go编码UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  pwd =abc123;我想在Go中找到相当于下面的C#代码。 
encoding = Encoding.UTF8;
SHA1 sha1 = SHA1.Create();
byte [] hash = sha1.ComputeHash(encoding.GetBytes(text));

我知道有一个 crypto / sha1 软件包。我知道我可以运行:

  pwd:=abc123
hasher:= sha1.New()// SHA1.Create();
hasher.Write([] byte(pwd))// sha1.ComputeHash但没有UTF8编码?

我不确定如何在散列时获得正确的编码。我想知道是否可以得到一些帮助来转换这个

解决方案

根据文档:


一个没有字节级转义的字符串文字总是保存有效的UTF-8
序列。


因此,如果在Golang源代码中,则不需要将字符串编码为utf8。但是,如果字符串来自输入,那么 utf8 包是您的朋友。


I am trying to find the equivalent of the following C# code in Go.

pwd = "abc123";
encoding = Encoding.UTF8;
SHA1 sha1 = SHA1.Create();
byte[] hash = sha1.ComputeHash(encoding.GetBytes(text));

I know that there is a crypto/sha1 package within Go. I know I can run:

pwd := "abc123"
hasher := sha1.New() // SHA1.Create();
hasher.Write([]byte(pwd)) // sha1.ComputeHash but without encoding in UTF8 ?

I am not sure how I can get the correct encoding when hashing. I was wondering if I could get some help converting this

解决方案

According to the documentation:

A string literal, absent byte-level escapes, always holds valid UTF-8 sequences.

So you don't need to encode into utf8 the string if is inside the Golang source code. However, if the string comes from an input, the utf8 package is your friend.

这篇关于用Go编码UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆