Go-标准库-strconv(二)
2023-06-13 09:18:47 时间
FormatFloat:将浮点型转换为字符串
FormatFloat函数将float64类型的数据转换为字符串,并允许指定转换的格式和精度。
package main
import (
"fmt"
"strconv"
)
func main() {
f := 3.1415926535
str := strconv.FormatFloat(f, 'E', -1, 64)
fmt.Printf("str is %s\n", str)
}
输出结果:
str is 3.1415926535E+00
Quote:为字符串添加引号
Quote函数将字符串添加上双引号,并且对特殊字符进行转义。
package main
import (
"fmt"
"strconv"
)
func main() {
str := "Hello, World!"
q := strconv.Quote(str)
fmt.Printf("q is %s\n", q)
}
输出结果:
csharpCopy codeq is "Hello, World!"
Append系列函数:将不同类型的数据转换为字符串并追加到字节数组中
Append系列函数将不同类型的数据转换为字符串并追加到字节数组中,函数名以Append开头,后面跟上转换的类型名称。
package main
import (
"fmt"
"strconv"
)
func main() {
str := "Hello, World!"
b := []byte(str)
b = strconv.AppendInt(b, 123, 10)
b = strconv.AppendBool(b, true)
b = strconv.AppendQuote(b, "Golang")
fmt.Printf("b is %s\n", b)
}
输出结果:
b is Hello, World!123true"Golang"
Can系列函数:判断字符串是否可以转换为指定的类型
Can系列函数用于判断字符串是否可以转换为指定的类型,函数名以Can开头,后面跟上转换的类型名称。
package main
import (
"fmt"
"strconv"
)
func main() {
str := "123"
if strconv.CanInt64(str) {
i64, _ := strconv.ParseInt(str, 10, 64)
fmt.Printf("i64 is %d\n", i64)
} else {
fmt.Println("conversion failed")
}
}
输出结果:
i64 is 123
相关文章
- Go数组中元素替换
- 【愚公系列】2022年07月 Go教学课程 026-结构体
- Go方法 为什么推荐 使用指针类型的接收者
- Go | 基本数据类型的相互转换
- 使用 Docker 搭建属于你自己的 Go Playground
- go的数据类型-基本数据类型-浮点型
- Go-标准库-flag(三)
- Go-标准库-testing(二)
- Go-标准库-context(二)
- Go-包管理-go mod(一)
- Go语言的核心Routine-Channel详解编程语言
- Go-连接Redis-学习go-redis包详解编程语言
- Go语言使用定时器实现任务队列
- Go语言使用select切换协程
- [下载] 微软Surface Hub 2S与Surface Laptop Go内置壁纸 横竖屏/4K支持
- 语言结合SQL Server,Go谱写新篇章(sqlserver的go)
- 给Oracle数据库添加专业技能,Go大发展(go_db_oracle)
- 语句使用Oracle中的Go语句来简化编程(oracle中go)