Golang strconv.FormatUint
最后修改于 2025 年 4 月 20 日
本教程解释了如何在 Go 中使用 strconv.FormatUint 函数。我们将通过实际示例介绍无符号整数到字符串转换的基础知识。
strconv.FormatUint 函数将无符号整数转换为字符串。它提供了灵活的格式化选项,包括不同的数字基数。
FormatUint 在给定基数下返回无符号整数的字符串表示形式。基数必须在 2 到 36 之间。对于基数 10,它类似于 fmt.Sprintf。
基本的 strconv.FormatUint 示例
strconv.FormatUint 最简单的用法是将无符号整数转换为基数 10 的字符串。这里我们演示了基本转换。
package main
import (
"fmt"
"strconv"
)
func main() {
num := uint64(42)
str := strconv.FormatUint(num, 10)
fmt.Printf("Unsigned integer %d converted to string '%s'\n", num, str)
}
我们将无符号整数 42 转换为基数 10 的字符串。该函数返回字符串表示形式,没有错误的可能性。
转换为不同的基数
strconv.FormatUint 支持转换为各种基数。此示例显示了十六进制、二进制和八进制转换。
package main
import (
"fmt"
"strconv"
)
func main() {
num := uint64(255)
fmt.Println("Base 10:", strconv.FormatUint(num, 10))
fmt.Println("Hexadecimal:", strconv.FormatUint(num, 16))
fmt.Println("Binary:", strconv.FormatUint(num, 2))
fmt.Println("Octal:", strconv.FormatUint(num, 8))
}
我们将相同的数字转换为不同的字符串表示形式。基数参数决定了转换使用的数字系统。
格式化大数字
FormatUint 可以处理非常大的无符号整数。此示例演示了最大 uint64 值的转换。
package main
import (
"fmt"
"math"
"strconv"
)
func main() {
maxUint64 := uint64(math.MaxUint64)
str := strconv.FormatUint(maxUint64, 10)
fmt.Printf("Maximum uint64 value: %d\n", maxUint64)
fmt.Printf("As string: %s\n", str)
fmt.Println("Length:", len(str))
}
我们将最大的 uint64 值转换为字符串。该函数可以毫无问题地处理 uint64 值的完整范围。
性能比较
此示例将 FormatUint 与 fmt.Sprintf 进行了比较,用于无符号整数格式化。
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
const iterations = 1000000
num := uint64(123456789)
// Benchmark FormatUint
start := time.Now()
for i := 0; i < iterations; i++ {
strconv.FormatUint(num, 10)
}
fmt.Println("FormatUint duration:", time.Since(start))
// Benchmark fmt.Sprintf
start = time.Now()
for i := 0; i < iterations; i++ {
fmt.Sprintf("%d", num)
}
fmt.Println("Sprintf duration:", time.Since(start))
}
对于简单的无符号整数转换,FormatUint 通常比 fmt.Sprintf 更快。在性能关键型代码中,这种差异很重要。
自定义数字格式化
此示例展示了如何使用 FormatUint 和附加的字符串操作来创建自定义数字格式化。
package main
import (
"fmt"
"strconv"
"strings"
)
func formatWithCommas(num uint64) string {
str := strconv.FormatUint(num, 10)
var parts []string
for i := len(str); i > 0; i -= 3 {
start := i - 3
if start < 0 {
start = 0
}
parts = append([]string{str[start:i]}, parts...)
}
return strings.Join(parts, ",")
}
func main() {
num := uint64(1234567890)
fmt.Println("Formatted with commas:", formatWithCommas(num))
}
我们将数字转换为字符串,然后添加逗号分隔符。这表明 FormatUint 可以是更复杂的格式化解决方案的一部分。
带前缀的十六进制
此示例展示了如何使用 FormatUint 创建带有“0x”前缀的十六进制字符串。
package main
import (
"fmt"
"strconv"
)
func main() {
num := uint64(255)
hexStr := "0x" + strconv.FormatUint(num, 16)
fmt.Println("Hexadecimal with prefix:", hexStr)
// Uppercase hexadecimal
upperHex := "0X" + strconv.FormatUint(num, 16)
fmt.Println("Uppercase hexadecimal:", upperHex)
}
我们将“0x”前缀与十六进制字符串连接起来。对于大写字母,我们可以改用“0X”前缀。
实际示例:文件权限
这个实际示例演示了如何使用 FormatUint 将 Unix 文件权限转换为八进制字符串。
package main
import (
"fmt"
"strconv"
)
func main() {
permissions := uint64(0644) // Common file permission
permStr := strconv.FormatUint(permissions, 8)
fmt.Printf("Permissions: %s (octal)\n", permStr)
fmt.Printf("Equivalent to: -rw-r--r--\n")
// Convert back to verify
parsed, _ := strconv.ParseUint(permStr, 8, 64)
fmt.Printf("Parsed back: %#o\n", parsed)
}
我们将文件权限转换为八进制字符串。该示例展示了这在系统编程上下文中的作用。
来源
本教程通过在各种场景下将无符号整数转换为字符串的实际示例,介绍了 Go 中的 strconv.FormatUint 函数。