Go: Convert byte slice (array) to string

Converting a slice of bytes to a string yields a string whose bytes are the elements of the slice:

b := []byte{'a', 'b', 'c', '\xe6', '\x97', '\xa5'}
s := string(b)
fmt.Println(s)
// Output: abc日

Arrays

If b is an array, slice it first using b[:]:

s := string(b[:])

Comments (2)

User avatar

I run into this error: cannot convert hashed (type [32]byte) to type string.

by nitohu |  Reply
User avatar

A [32]byte is an array. Convert it to a slice first: string(hashes[:]).

by Andreas Lundblad |  Reply

Add comment