Golang strconv.AppendBool()函数及示例
Go语言通过 strconv包 提供了内置的支持,可以实现基本数据类型的字符串表示形式的转换。该包提供了一个 AppendBool()函数 ,用于根据num2的值将bool(即true或false)追加到num1中,并返回扩展的缓冲区,如语法中所示。要访问AppendBool()函数,您需要在程序中导入strconv包。
语法:
func AppendBool(num1 []byte, num2 bool) []byte
示例1:
// Golang程序演示
// strconv.AppendBool()函数
package main
import (
"fmt"
"strconv"
)
func main() {
// 使用AppendBool()函数
val := []byte("Is Bool: ")
val = strconv.AppendBool(val, true)
fmt.Println(string(val))
}
输出:
Is Bool: true
示例2:
// Golang程序演示
// strconv.AppendBool()函数
package main
import (
"fmt"
"strconv"
)
func main() {
// 使用AppendBool()函数
val := []byte("Append Bool:")
fmt.Println(string(val))
fmt.Println("Length(Before): ", len(val))
fmt.Println("Capacity(Before): ", cap(val))
val = strconv.AppendBool(val, true)
fmt.Println(string(val))
fmt.Println("Length(After): ", len(val))
fmt.Println("Capacity(After): ", cap(val))
}
输出:
Append Bool:
Length(Before): 12
Capacity(Before): 12
Append Bool:true
Length(After): 16
Capacity(After): 32