The strconv package is also a package that we often use in Go language. It is usually used for converting between strings and basic data types. It provides a series of functions for parsing and formatting string operations.
Brief Introduction#
Here is a brief introduction to some commonly used functions in the strconv package:
-
strconv.Itoa: Converts an integer to a string. It takes an integer value and returns the string representation of that integer.
Example:
import ( "fmt" "strconv" ) func main() { num := 42 str := strconv.Itoa(num) fmt.Println(str) // Output: "42" }
-
strconv.Atoi: Converts a string to an integer. It takes a string representing an integer and returns the corresponding integer value. If the conversion fails, it returns an error message.
Example:
import ( "fmt" "strconv" ) func main() { str := "42" num, err := strconv.Atoi(str) if err != nil { fmt.Println("Conversion failed:", err) return } fmt.Println(num) // Output: 42 }
-
strconv.ParseFloat: Converts a string to a floating-point number. It takes a string representing a floating-point number, a specified precision (32 or 64), and the corresponding floating-point type, and returns the corresponding floating-point value. If the conversion fails, it returns an error message.
Example:
import ( "fmt" "strconv" ) func main() { str := "3.14" num, err := strconv.ParseFloat(str, 64) if err != nil { fmt.Println("Conversion failed:", err) return } fmt.Println(num) // Output: 3.14 }
-
strconv.FormatInt / strconv.FormatFloat: Converts an integer or floating-point number to a string. It takes an integer or floating-point value, as well as a specified base (such as decimal, hexadecimal, etc.), and returns the corresponding string representation.
Example:
import ( "fmt" "strconv" ) func main() { num := 42 str := strconv.FormatInt(int64(num), 10) fmt.Println(str) // Output: "42" pi := 3.14159 str = strconv.FormatFloat(pi, 'f', 2, 64) fmt.Println(str) // Output: "3.14" }
These are just some commonly used functions provided by the strconv package. It also provides other functions for more complex conversion operations, such as handling boolean values, Unicode characters, etc.
Brief Introduction to AppendXXX#
The AppendXXX
functions are used to append values of basic types to byte slices ([]byte
).
-
AppendBool
functionfunc AppendBool(dst []byte, b bool) []byte
The
AppendBool
function converts the boolean valueb
to its string representation and appends the result to the byte slicedst
. The return value is the appended byte slice.Example:
var dst []byte dst = strconv.AppendBool(dst, true) fmt.Println(string(dst)) // Output: "true"
-
AppendFloat
functionfunc AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte
The
AppendFloat
function converts the floating-point numberf
to its string representation and appends the result to the byte slicedst
. Thefmt
parameter specifies the format, theprec
parameter specifies the precision, and thebitSize
parameter specifies the floating-point number size.Example:
var dst []byte dst = strconv.AppendFloat(dst, 3.14159, 'f', 4, 64) fmt.Println(string(dst)) // Output: "3.1416"
Please note that these AppendXXX
functions are used to append string representations of basic type values to byte slices, rather than modifying values in existing byte slices. These functions are typically used when you need to convert a basic type value to a string and append it to a byte slice.
Brief Introduction to QuoteXXX#
The QuoteXXX
functions are a group of functions in the strconv
package that are used to convert values of basic types to their source code representations in Go language and return the string representation of that representation.
Here are the purposes and examples of the QuoteXXX
functions:
-
Quote
functionfunc Quote(s string) string
The
Quote
function converts the strings
to its source code representation in Go language and returns the string representation of that representation. It escapes special characters so that the string can be used directly in source code.Example usage:
str := `Hello, "Gopher"` quoted := strconv.Quote(str) fmt.Println(quoted) // Output: "Hello, \"Gopher\""
-
QuoteRune
functionfunc QuoteRune(r rune) string
The
QuoteRune
function converts the Unicode characterr
to its source code representation in Go language and returns the string representation of that representation.Example usage:
rune := '♥' quoted := strconv.QuoteRune(rune) fmt.Println(quoted) // Output: "'♥'"
-
QuoteRuneToASCII
functionfunc QuoteRuneToASCII(r rune) string
The
QuoteRuneToASCII
function converts the Unicode characterr
to its source code representation in Go language and returns the ASCII string representation of that representation. It escapes non-ASCII characters.Example usage:
rune := '♥' quoted := strconv.QuoteRuneToASCII(rune) fmt.Println(quoted) // Output: "'\\u2665'"
-
QuoteRuneToGraphic
functionfunc QuoteRuneToGraphic(r rune) string
The
QuoteRuneToGraphic
function converts the Unicode characterr
to its source code representation in Go language and returns the string representation of printable characters in that representation. It escapes non-graphic characters.Example usage:
rune := '\u2022' // Chinese "dot" character quoted := strconv.QuoteRuneToGraphic(rune) fmt.Println(quoted) // Output: "'•'"
These QuoteXXX
functions can be used to generate the representation of source code strings, especially when you need to embed strings or characters in source code, to ensure that their representations are correct and do not cause syntax errors.
Summary#
The strconv package is a package in the Go standard library that is used for converting between strings and basic data types. It provides a series of functions, including functions for converting basic types to strings and functions for converting strings to basic types.
The commonly used functions and features of this package include:
-
Parse functions: Used to parse strings into values of basic types, such as
ParseBool
,ParseInt
,ParseFloat
, etc. -
Format functions: Used to format values of basic types into string representations, such as
FormatBool
,FormatInt
,FormatFloat
, etc. -
Append functions: Used to append values of basic types to byte slices, such as
AppendBool
,AppendInt
,AppendFloat
, etc. -
Quote functions: Used to convert values of basic types to their source code representations in Go language as strings, such as
Quote
,QuoteRune
,QuoteRuneToASCII
, etc. -
String conversion functions: Including
Atoi
,Itoa
,ParseUint
,FormatUint
, etc., used for handling conversions between strings and integer types. -
Conversion between strings and floating-point types: Including
Atof
,FormatFloat
,ParseFloat
, etc., used for handling conversions between strings and floating-point types.
The strconv package provides a flexible and convenient way to handle conversions between strings and basic data types, making it easy for us to perform data format conversions and processing.