How to convert byte array to string in Golang
In Go, you can convert a byte array to a string using the string()
function. This function takes a byte array as an argument and returns a string. You can also use the fmt.Sprintf()
function to convert a byte array to a string.`
Convert byte array to string using string()
The string()
function takes a byte array as an argument and returns a string. The following example shows how to convert a byte array to a string using the string()
function.
The output of the above program is:
The output of the above program is:
Convert byte array to string using fmt.Sprintf()
The fmt.Sprintf()
function takes a format string and a list of arguments as arguments and returns a string. The following example shows how to convert a byte array to a string using the fmt.Sprintf()
function.
The output of the above program is:
Convert byte array to string using a slice of bytes
You can also convert a byte array to a string by slicing the byte array.
The output of the above program is:
Conclusion
In this article, we learned how to convert a byte array to a string in Go. We learned how to convert a byte array to a string using the string()
function, the fmt.Sprintf()
function, and by slicing the byte array.