Golang 中的 strconv 包常用函数及用法详解

 更新时间:2023年06月29日 16:03:43   作者:路多辛  
strconv是Golang中一个非常常用的包,主要用于字符串和基本数据类型之间的相互转换,这篇文章主要介绍了Golang中的strconv包,需要的朋友可以参考下

strconv 是 Golang 中一个非常常用的包,主要用于字符串和基本数据类型之间的相互转换。本文将详细介绍 strconv 包的常用函数及用法。

strconv.Atoi 和 strconv.Itoa

Atoi 函数用于将字符串转换为 int 类型,Itoa 函数则用于将 int 类型转换为字符串类型。简单使用示例如下:

package main
import (
    "fmt"
    "strconv"
)
func main() {
    str := "123"
    intValue, _ := strconv.Atoi(str)
    fmt.Printf("str to int: %d\n", intValue)
    intValue += 1
    str = strconv.Itoa(intValue)
    fmt.Printf("int to str: %s\n", str)
}

strconv.Parse 系列函数

strconv.Parse 系列函数用于将字符串解析为指定类型。其中常用的函数有 ParseInt、ParseBool 和 ParseFloat。简单使用示例如下:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	// 解析整数
	intStr := "123"
	intValue, _ := strconv.ParseInt(intStr, 10, 64)
	fmt.Printf("Parsed int value: %d\n", intValue)
	// 解析布尔值
	boolStr := "true"
	boolValue, _ := strconv.ParseBool(boolStr)
	fmt.Printf("Parsed bool value: %t\n", boolValue)
	// 解析浮点数
	floatStr := "3.14"
	floatValue, _ := strconv.ParseFloat(floatStr, 64)
	fmt.Printf("Parsed float value: %f\n", floatValue)
}

strconv.Format 系列函数

strconv.Format 系列函数用于将基本数据类型转换为字符串类型。常用的函数有 FormatInt、FormatBool 和 FormatFloat。简单使用示例如下:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	// 格式化整数
	intValue := 123
	intStr := strconv.FormatInt(int64(intValue), 10)
	fmt.Printf("Formatted int string: %s\n", intStr)
	// 格式化布尔值
	boolValue := true
	boolStr := strconv.FormatBool(boolValue)
	fmt.Printf("Formatted bool string: %s\n", boolStr)
	// 格式化浮点数
	floatValue := 3.14
	floatStr := strconv.FormatFloat(floatValue, 'f', -1, 64)
	fmt.Printf("Formatted float string: %s\n", floatStr)
}

strconv.Append 系列函数

strconv.Append 系列函数用于将基本数据类型追加到已存在的字节数组中。常用的函数有 AppendInt、AppendBool 和 AppendFloat。简单使用示例如下:

package main
import (
    "fmt"
    "strconv"
)
func main() {
    // 追加整数到字节数组
    num1 := 123
    byteSlice := []byte("Number: ")
    byteSlice = strconv.AppendInt(byteSlice, int64(num1), 10)
    fmt.Printf("Appended int: %s\n", byteSlice)
    // 追加布尔值到字节数组
    boolVal := true
    byteSlice = []byte("Bool: ")
    byteSlice = strconv.AppendBool(byteSlice, boolVal)
    fmt.Printf("Appended bool: %s\n", byteSlice)
    // 追加浮点数到字节数组
    floatVal := 3.14
    byteSlice = []byte("Float: ")
    byteSlice = strconv.AppendFloat(byteSlice, floatVal, 'f', -1, 64)
    fmt.Printf("Appended float: %s\n", byteSlice)
}

strconv.IsPrint 和 strconv.IsGraphic

strconv.IsPrint 函数用于判断给定的 Unicode 字符是否可打印,可打印字符是指那些可以在屏幕上显示的字符。strconv.IsGraphic 函数用于判断给定的 Unicode 字符是否是图形字符,图形字符是指可视化的字符。简单使用示例如下:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	chars := []rune{'H', 'e', 'l', '\n', '♥', 127}
	for _, char := range chars {
		fmt.Printf("Character: %c, IsPrint: %v\n", char, strconv.IsPrint(char))
		fmt.Printf("Character: %c, IsGraphic: %v\n", char, strconv.IsGraphic(char))
	}
}

strconv.Quote 和 strconv.Unquote 系列函数

strconv.Quote 系列函数用于转义和引用字符串的功能,将字符串转换为可以直接表示的字符串字面值(literal),包括添加转义字符和引号。简单使用示例如下:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	str := `路多辛的, "所思所想"!`
	quoted := strconv.Quote(str)
	fmt.Println("Quoted: ", quoted)
	unquoted, err := strconv.Unquote(quoted)
	if err != nil {
		fmt.Println("Unquote error: ", err)
	} else {
		fmt.Println("Unquoted: ", unquoted)
	}
}

strconv.CanBackquote

strconv.CanBackquote 函数用于检查字符串是否可以不变地表示为单行反引号字符串(即以 `` 开头和结尾的字符串)。简单使用示例如下:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	str1 := "Hello, world!"
	str2 := "`Hello, world!`"
	str3 := "`Hello,\nworld!`"
	fmt.Println(strconv.CanBackquote(str1)) // 输出:false
	fmt.Println(strconv.CanBackquote(str2)) // 输出:true
	fmt.Println(strconv.CanBackquote(str3)) // 输出:false
}

到此这篇关于Golang 中的 strconv 包详解的文章就介绍到这了,更多相关Golang strconv 包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用go连接clickhouse方式

    使用go连接clickhouse方式

    这篇文章主要介绍了使用go连接clickhouse方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • Goland远程连接Linux进行项目开发的实现

    Goland远程连接Linux进行项目开发的实现

    有的时候我们的开发代码要在linux服务器上运行,本文主要介绍了Goland远程连接Linux进行项目开发的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-06-06
  • 详解Go语言如何进行Http调用

    详解Go语言如何进行Http调用

    无论是微服务还是单体架构等,服务间都有相互通信的时候,而最直接的通信方法就是 HTTP 调用,本文将会介绍在 Go 语言里,如何进行 HTTP 调用,需要的可以参考一下
    2022-12-12
  • 聊聊Golang的语言结构和变量问题

    聊聊Golang的语言结构和变量问题

    这篇文章主要介绍了Golang的语言结构和变量问题,在golang中定义变量的一般形式是使用 var 关键字,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-11-11
  • Go语言实现本地缓存的策略详解

    Go语言实现本地缓存的策略详解

    今天给大家分享的是Go语言本地缓存的一些内容,主要是结合bigcache和fastcache两个优秀的开源代码库,总结一些设计思路和感悟,文章通过代码示例介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • Go语言操作redis数据库的方法

    Go语言操作redis数据库的方法

    这篇文章主要介绍了Go语言操作redis数据库的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • Go-客户信息关系系统的实现

    Go-客户信息关系系统的实现

    这篇文章主要介绍了Go-客户信息关系系统的实现,本文章内容详细,具有很好的参考价值,希望对大家有所帮助,需要的朋友可以参考下
    2023-01-01
  • Go语言的反射reflect使用大全

    Go语言的反射reflect使用大全

    Go语言中reflect包提供了运行时反射的功能,本文主要介绍了Go语言的反射reflect使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-08-08
  • Go语言使用Zap轻松搞定结构化日志

    Go语言使用Zap轻松搞定结构化日志

    在 Go 语言中,有许多日志库可供选择,但在性能和灵活性方面,Zap 是其中的佼佼者,下面我们就来看看Go 项目中如何使用 Zap 进行结构化日志记录吧
    2024-11-11
  • Golang logrus 日志包及日志切割的实现

    Golang logrus 日志包及日志切割的实现

    这篇文章主要介绍了Golang logrus 日志包及日志切割的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02

最新评论