Go 语言提供内置支持,以通过 strconv 包实现与基本数据类型的字符串表示之间的转换。这个包提供了一个AppendUint()函数,用于将 FormatUint 生成的无符号整数 x 的字符串形式附加到 num 并返回扩展缓冲区。或者换句话说,该函数将 uint 类型整数 x 转换为字符串并将其附加到 num 的末尾。要访问AppendUint()函数,您需要在程序中导入 strconv 包。
句法:
func AppendUint(num []byte, x uint64, base int) []byte
示例 1:
// Golang program to illustrate
// strconv.AppendUint() function
package main
import (
"fmt"
"strconv"
)
func main() {
// Converting the unit
// type integer x to a string
// appends it to the end of
// the given []byte
// Using AppendUint() function
val1 := []byte("uint value (base 16): ")
val1 = strconv.AppendUint(val1, 35, 16)
fmt.Println(string(val1))
val2 := []byte("uint value (base 10): ")
val2 = strconv.AppendUint(val2, 35, 10)
fmt.Println(string(val2))
}
输出:
uint value (base 16): 23
uint value (base 10): 35
示例 2:
// Golang program to illustrate
// strconv.AppendUint() function
package main
import (
"fmt"
"strconv"
)
func main() {
// Converting the unit
// type integer x to a string
// appends it to the
// end of the given []byte
// Using AppendUint() function
val1 := []byte("uint value (base 16): ")
val1 = strconv.AppendUint(val1, 45, 16)
fmt.Println(string(val1))
fmt.Println("Length: ", len(val1))
fmt.Println("Capacity: ", cap(val1))
val2 := []byte("uint value (base 10): ")
val2 = strconv.AppendUint(val2, 43, 10)
fmt.Println(string(val2))
fmt.Println("Length: ", len(val2))
fmt.Println("Capacity: ", cap(val2))
}
输出:
uint value (base 16): 2d
Length: 24
Capacity: 48
uint value (base 10):43
Length: 23
Capacity: 48