golang开发 string 使用方法
在 Go 中,将一个变量转换为字符串类型需要使用类型断言。即使用.(string)将变量转换为字符串类型。
示例代码:
var num int = 123
str := fmt.Sprintf("%d", num) // 使用 fmt.Sprintf 将数字转换为字符串
fmt.Println(str)
var value interface{} = "Hello, world!"
str2, ok := value.(string) // 使用类型断言将 value 转换为字符串类型
if ok {
fmt.Println(str2)
} else {
fmt.Println("value 不是字符串类型")
}
输出结果:
123
Hello, world!
原文地址: https://www.cveoy.top/t/topic/craZ 著作权归作者所有。请勿转载和采集!