欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Golang?中的?strconv?包常用函數(shù)及用法詳解

 更新時間:2023年06月29日 16:03:43   作者:路多辛  
strconv是Golang中一個非常常用的包,主要用于字符串和基本數(shù)據(jù)類型之間的相互轉(zhuǎn)換,這篇文章主要介紹了Golang中的strconv包,需要的朋友可以參考下

strconv 是 Golang 中一個非常常用的包,主要用于字符串和基本數(shù)據(jù)類型之間的相互轉(zhuǎn)換。本文將詳細介紹 strconv 包的常用函數(shù)及用法。

strconv.Atoi 和 strconv.Itoa

Atoi 函數(shù)用于將字符串轉(zhuǎn)換為 int 類型,Itoa 函數(shù)則用于將 int 類型轉(zhuǎn)換為字符串類型。簡單使用示例如下:

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 系列函數(shù)

strconv.Parse 系列函數(shù)用于將字符串解析為指定類型。其中常用的函數(shù)有 ParseInt、ParseBool 和 ParseFloat。簡單使用示例如下:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	// 解析整數(shù)
	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)
	// 解析浮點數(shù)
	floatStr := "3.14"
	floatValue, _ := strconv.ParseFloat(floatStr, 64)
	fmt.Printf("Parsed float value: %f\n", floatValue)
}

strconv.Format 系列函數(shù)

strconv.Format 系列函數(shù)用于將基本數(shù)據(jù)類型轉(zhuǎn)換為字符串類型。常用的函數(shù)有 FormatInt、FormatBool 和 FormatFloat。簡單使用示例如下:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	// 格式化整數(shù)
	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)
	// 格式化浮點數(shù)
	floatValue := 3.14
	floatStr := strconv.FormatFloat(floatValue, 'f', -1, 64)
	fmt.Printf("Formatted float string: %s\n", floatStr)
}

strconv.Append 系列函數(shù)

strconv.Append 系列函數(shù)用于將基本數(shù)據(jù)類型追加到已存在的字節(jié)數(shù)組中。常用的函數(shù)有 AppendInt、AppendBool 和 AppendFloat。簡單使用示例如下:

package main
import (
    "fmt"
    "strconv"
)
func main() {
    // 追加整數(shù)到字節(jié)數(shù)組
    num1 := 123
    byteSlice := []byte("Number: ")
    byteSlice = strconv.AppendInt(byteSlice, int64(num1), 10)
    fmt.Printf("Appended int: %s\n", byteSlice)
    // 追加布爾值到字節(jié)數(shù)組
    boolVal := true
    byteSlice = []byte("Bool: ")
    byteSlice = strconv.AppendBool(byteSlice, boolVal)
    fmt.Printf("Appended bool: %s\n", byteSlice)
    // 追加浮點數(shù)到字節(jié)數(shù)組
    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 函數(shù)用于判斷給定的 Unicode 字符是否可打印,可打印字符是指那些可以在屏幕上顯示的字符。strconv.IsGraphic 函數(shù)用于判斷給定的 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 系列函數(shù)

strconv.Quote 系列函數(shù)用于轉(zhuǎn)義和引用字符串的功能,將字符串轉(zhuǎn)換為可以直接表示的字符串字面值(literal),包括添加轉(zhuǎn)義字符和引號。簡單使用示例如下:

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 函數(shù)用于檢查字符串是否可以不變地表示為單行反引號字符串(即以 `` 開頭和結(jié)尾的字符串)。簡單使用示例如下:

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
}

到此這篇關(guān)于Golang 中的 strconv 包詳解的文章就介紹到這了,更多相關(guān)Golang strconv 包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用go連接clickhouse方式

    使用go連接clickhouse方式

    這篇文章主要介紹了使用go連接clickhouse方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Goland遠程連接Linux進行項目開發(fā)的實現(xiàn)

    Goland遠程連接Linux進行項目開發(fā)的實現(xiàn)

    有的時候我們的開發(fā)代碼要在linux服務器上運行,本文主要介紹了Goland遠程連接Linux進行項目開發(fā)的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • 詳解Go語言如何進行Http調(diào)用

    詳解Go語言如何進行Http調(diào)用

    無論是微服務還是單體架構(gòu)等,服務間都有相互通信的時候,而最直接的通信方法就是 HTTP 調(diào)用,本文將會介紹在 Go 語言里,如何進行 HTTP 調(diào)用,需要的可以參考一下
    2022-12-12
  • 聊聊Golang的語言結(jié)構(gòu)和變量問題

    聊聊Golang的語言結(jié)構(gòu)和變量問題

    這篇文章主要介紹了Golang的語言結(jié)構(gòu)和變量問題,在golang中定義變量的一般形式是使用 var 關(guān)鍵字,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2021-11-11
  • Go語言實現(xiàn)本地緩存的策略詳解

    Go語言實現(xiàn)本地緩存的策略詳解

    今天給大家分享的是Go語言本地緩存的一些內(nèi)容,主要是結(jié)合bigcache和fastcache兩個優(yōu)秀的開源代碼庫,總結(jié)一些設計思路和感悟,文章通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • Go語言操作redis數(shù)據(jù)庫的方法

    Go語言操作redis數(shù)據(jù)庫的方法

    這篇文章主要介紹了Go語言操作redis數(shù)據(jù)庫的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • Go-客戶信息關(guān)系系統(tǒng)的實現(xiàn)

    Go-客戶信息關(guān)系系統(tǒng)的實現(xiàn)

    這篇文章主要介紹了Go-客戶信息關(guān)系系統(tǒng)的實現(xiàn),本文章內(nèi)容詳細,具有很好的參考價值,希望對大家有所幫助,需要的朋友可以參考下
    2023-01-01
  • Go語言的反射reflect使用大全

    Go語言的反射reflect使用大全

    Go語言中reflect包提供了運行時反射的功能,本文主要介紹了Go語言的反射reflect使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-08-08
  • Go語言使用Zap輕松搞定結(jié)構(gòu)化日志

    Go語言使用Zap輕松搞定結(jié)構(gòu)化日志

    在?Go?語言中,有許多日志庫可供選擇,但在性能和靈活性方面,Zap?是其中的佼佼者,下面我們就來看看Go?項目中如何使用?Zap?進行結(jié)構(gòu)化日志記錄吧
    2024-11-11
  • Golang logrus 日志包及日志切割的實現(xiàn)

    Golang logrus 日志包及日志切割的實現(xiàn)

    這篇文章主要介紹了Golang logrus 日志包及日志切割的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02

最新評論