In Golang, strconv.Itoa()
is used to convert an integer to a string. If you want to convert a byte slice to a string using strconv.Itoa()
, you first need to convert the byte slice to an integer and then convert the integer to a string.
Here's an example of how you can achieve it:
package main
import (
"fmt"
"strconv"
)
func main() {
// Example byte slice
b := []byte{49, 50, 51} // corresponds to "123" in ASCII
// Convert byte slice to integer
num, err := strconv.Atoi(string(b))
if err != nil {
fmt.Println("Failed to convert byte slice to integer:", err)
return
}
// Convert integer to string using strconv.Itoa()
str := strconv.Itoa(num)
fmt.Println("Converted string:", str) // Output: 123
}
In the above code, strconv.Atoi()
is used to convert the byte slice b
to an integer num
. Then, strconv.Itoa()
is used to convert num
back to a string str
. Finally, str
is printed, which will give you the expected result.