Detailed explanation of strconv package in Golang

strconv is a very commonly used package in Golang, mainly used for conversion between strings and basic data types. This article will introduce the common functions and usage of the strconv package in detail.

strconv.Atoi and strconv.Itoa

Atoi function is used to convert string to int type, and Itoa function is used to convert int type to string type. A simple usage example is as follows:

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 series functions

The strconv.Parse series of functions are used to parse a string into a specified type. Among the commonly used functions are ParseInt, ParseBool and ParseFloat. A simple usage example is as follows:

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 series of functions

The strconv.Format series of functions are used to convert basic data types to string types. Commonly used functions are FormatInt, FormatBool, and FormatFloat. A simple usage example is as follows:

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 series functions

The strconv.Append series of functions are used to append basic data types to an existing byte array. Commonly used functions are AppendInt, AppendBool and AppendFloat. A simple usage example is as follows:

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

The strconv.IsPrint function is used to determine whether a given Unicode character is printable, and printable characters refer to those characters that can be displayed on the screen. The strconv.IsGraphic function is used to determine whether a given Unicode character is a graphic character, and a graphic character refers to a visual character. A simple usage example is as follows:

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 and strconv.Unquote series functions

The strconv.Quote series of functions are used for escaping and quoting string functions, converting a string into a string literal value (literal) that can be directly represented, including adding escape characters and quotation marks. A simple usage example is as follows:

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

The strconv.CanBackquote function is used to check whether a string can be represented unchanged as a single-line backquoted string (ie, a string beginning and ending with ``). A simple usage example is as follows:

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
}

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131445019