Whats the equivalent of C#'s Encoding.UTF8.GetString in Go?
As i already know that Go's default encoding is in UTF8 and string(somebytes) in Go would produce a UTF8 encoded string.
C#:
public static void Main()
{
byte[] bytes = new byte[] { 144, 197, 217, 192, 204, 249, 181, 42, 92, 252, 243, 87, 170, 243, 169, 80, 175, 112, 192, 239};
string str = Encoding.UTF8.GetString(bytes);
Console.WriteLine(str);
}
Go:
func main() {
bytes := []byte { 144, 197, 217, 192, 204, 249, 181, 42, 92, 252, 243, 87, 170, 243, 169, 80, 175, 112, 192, 239}
str := string(bytes)
fmt.Println(str)
}
C# code produces:
�������*\��W��P�p��
Go code produces:
�������*\��W���P�p��
what I am missing here?
Clearly, whatever way you look at it, your bytes
are not valid UTF-8.
For example,
package main
import (
"fmt"
)
func main() {
bytes := []byte{144, 197, 217, 192, 204, 249, 181, 42, 92, 252, 243, 87, 170, 243, 169, 80, 175, 112, 192, 239}
fmt.Println(len(bytes))
fmt.Printf("%v
", bytes)
fmt.Printf("% x
", bytes)
fmt.Printf("%q
", bytes)
fmt.Printf("%s
", bytes)
}
Playground: https://play.golang.org/p/bHhkeGuZcCK
Output:
20
[144 197 217 192 204 249 181 42 92 252 243 87 170 243 169 80 175 112 192 239]
90 c5 d9 c0 cc f9 b5 2a 5c fc f3 57 aa f3 a9 50 af 70 c0 ef
"\x90\xc5\xd9\xc0\xcc\xf9\xb5*\\\xfc\xf3W\xaa\xf3\xa9P\xafp\xc0\xef"
�������*\��W���P�p��
References:
Unicode: UTF-8, UTF-16, UTF-32 & BOM