Convert string to/from byte slice

String to byte slice

When you convert a string to a byte slice, you get a slice that contains the bytes of the string.

fmt.Println([]byte("abc日")) // [97 98 99 230 151 165]

Byte slice 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)
// Output: abc日

Further reading

String functions cheat sheet

Share this page: