如何将 uint8 切片转换为字符串

Le *_*ang 6 type-conversion go

[]uint8从 转换为的最佳方式是什么string

我正在使用http://github.com/confluenceinc/confluence-kafka-go/kafka

从 kafka 读取事件。但它不返回纯字符串事件。它返回类型为 的事件[]uint8。我怎样才能将此事件从 转换[]uint8string

Fli*_*mzy 17

byte是 的别名uint8uint8,这意味着) (又名)的切片也是(又名)[]uint8的切片。byte[]byte

并且字节切片和字符串是可以直接转换的,因为字符串是由字节切片支持的:

myByteSlice := []byte{ ... }     // same as myByteSlice := []uint8{ ... }
myString := string(myByteSlice)  // myString is a string representation of the byte slice
myOtherSlice := []byte(myString) // Converted back to byte slice
Run Code Online (Sandbox Code Playgroud)