SoFunction
Updated on 2025-03-05

Detailed explanation of common functions and usage of strconv package in Golang

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

and

The Atoi function is used to convert a string to an int type, and the Itoa function is used to convert a int type to a string type. Simple usage examples are as follows:

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

Series functions

Series functions are used to parse strings to specified types. Commonly used functions are ParseInt, ParseBool and ParseFloat. Simple usage examples are as follows:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	// parse integers	intStr := "123"
	intValue, _ := (intStr, 10, 64)
	("Parsed int value: %d\n", intValue)
	// parse boolean values	boolStr := "true"
	boolValue, _ := (boolStr)
	("Parsed bool value: %t\n", boolValue)
	// parse floating point numbers	floatStr := "3.14"
	floatValue, _ := (floatStr, 64)
	("Parsed float value: %f\n", floatValue)
}

Series functions

Series functions are used to convert basic data types to string types. Commonly used functions are FormatInt, FormatBool and FormatFloat. Simple usage examples are as follows:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	// Format integers	intValue := 123
	intStr := (int64(intValue), 10)
	("Formatted int string: %s\n", intStr)
	// Format boolean values	boolValue := true
	boolStr := (boolValue)
	("Formatted bool string: %s\n", boolStr)
	// Format floating point numbers	floatValue := 3.14
	floatStr := (floatValue, 'f', -1, 64)
	("Formatted float string: %s\n", floatStr)
}

Series functions

Series functions are used to append basic data types to an existing byte array. Commonly used functions are AppendInt, AppendBool and AppendFloat. Simple usage examples are as follows:

package main
import (
    "fmt"
    "strconv"
)
func main() {
    // Append integers to byte array    num1 := 123
    byteSlice := []byte("Number: ")
    byteSlice = (byteSlice, int64(num1), 10)
    ("Appended int: %s\n", byteSlice)
    // Append boolean value to byte array    boolVal := true
    byteSlice = []byte("Bool: ")
    byteSlice = (byteSlice, boolVal)
    ("Appended bool: %s\n", byteSlice)
    // Append floating point numbers to byte array    floatVal := 3.14
    byteSlice = []byte("Float: ")
    byteSlice = (byteSlice, floatVal, 'f', -1, 64)
    ("Appended float: %s\n", byteSlice)
}

and

Functions are used to determine whether a given Unicode character is printable. Printable characters refer to those characters that can be displayed on the screen. Functions are used to determine whether a given Unicode character is a graphic character, which refers to a visual character. Simple usage examples are as follows:

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

and series functions

Series functions are used to escape and quote strings, converting strings into string literals that can be represented directly, including adding escape characters and quotes. Simple usage examples are as follows:

package main
import (
	"fmt"
	"strconv"
)
func main() {
	str := `Ludoshin's, "What you think"!`
	quoted := (str)
	("Quoted: ", quoted)
	unquoted, err := (quoted)
	if err != nil {
		("Unquote error: ", err)
	} else {
		("Unquoted: ", unquoted)
	}
}

Functions are used to check whether a string can be represented invariably as a single-line backtick string (i.e., a string that starts and ends with ``). Simple usage examples are as follows:

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

This is the end of this article about the detailed explanation of the strconv package in Golang. For more related content of the strconv package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!