Convert int32 to string in Golang
Converting int32 to string in Golang can be straightforward with a concise solution: fmt.Sprint(i). However, if this direct method is not sufficient, there are several conversion options available:
Performance benchmarks indicate that the custom conversion function (String) is the most efficient, followed by strconv.FormatInt, strconv.Itoa, and fmt.Sprint.
Here's a code snippet demonstrating the comparison:
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
var s string
i := int32(-2147483648)
t := time.Now()
for j := 0; j ), i/10
if i == 0 {
if signed {
pos--
buf[pos] = '-'
}
return string(buf[pos:])
}
}
}
Ultimately, the best choice depends on the specific requirements and performance constraints of your application.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3