Preface
Previous articleLearn about the common functions and methods of Go standard library strings in one articleIntroductionstrings
Some commonly used functions and methods in the database are also used in this articlestring
Type-centric, bystrconv
Standard library, introducing functions that convert with other basic data types.
strconv
strconv
Package providedstring
Functions that convert between types and other basic data types will not be introduced in this article. If you want to know more, you can go toGo package documentationCheck.
Conversion between string and int
Itoa: int type to string type
Itoa(i int) string
- parameter
i
is the number to be converted. - The return value is the converted string.
import ( "fmt" "strconv" ) func main() { num := 16 str := (num) ("Type: %T, value: %s", str, str) // Type: string, value: 16}
Atoi: string type to int type
Atoi(s string) (int, error)
- The parameter of the function is the string to be converted
- There are two return values, the first is the converted plastic number, and the second is the wrong description.
import ( "fmt" "strconv" ) func main() { str := "666" num, err := (str) if err != nil { ("err: ", ()) return } ("Type: %T, value: %d", num, num) // Type: int, value: 666}
This function isItoa
Compared to the function, there is one moreerror
Return value because if the string is passed in, it cannot be converted into a number, for example1a1
, this function will returnNumError
Error, andItoa
Functions, no matter what kind of number they are passed in, can be converted tostring
, so there is noerror
The statement.
FormatInt: Converts a number to a specified binary number and returns as string type
FormatInt(i int64, base int) string
- The first parameter is the specified number, the type is
int64
- The second parameter is the specified binary
- The third parameter is a string after converting the number into a specified number.
import ( "strconv" ) func main() { var num int64 = 2 str := (num, 2) var num2 int64 = 17 str2 := (num2, 16) println(str) // 10 println(str2) // 11 }
The above code implements the number2
Convert to binary form10
, turn the number17
Convert to hexadecimal form11
, and return as string type. All the above numbers represent signsint64
Type, corresponding to it is unsigneduint64
Convert function of typeFormatUint
。
ParseInt: Given the cardinality (digital number) and digits, return the corresponding decimal value.
ParseInt(s string, base int, bitSize int) (i int64, err error)
- The first parameter
s
For the string to be parsed - The second parameter
base
is the cardinality, that is, the0
、2
arrive36
Category. - The third parameter
bitSize
is the number of digits,0
、8
、16
、32
、64
Corresponding to the bitsint
、int8
、int16
、int32
、int64
. ifbitSize
Less than0
Or greater than64
, an error is returned. - The first return value
i
The number after conversion - The second return value
err
For error messages generated during conversion, exceptbitSize
Less than0
Or greater than64
, an error will occur except if the passed strings
andbase
Parameter orbitSize
If the parameters do not match, an error will also occur.
import ( "fmt" "strconv" ) func main() { parseInt, err := ("100", 2, 64) if err != nil { (()) return } println(parseInt) // 4 parseInt2, err := ("666", 2, 64) if err != nil { (()) // : parsing "666": invalid syntax return } println(parseInt2) }
First, put the binary100
Convert to decimal system,4
, and then binary666
Convert to decimal, but the number corresponding to the binary does not have 666, so the conversion error is returned: parsing "666": invalid syntax
error message. Corresponding to this is the return unsigned bituint64
Convert function of typeParseUint
。
Conversion between string and float
ParseFloat: String type to floating point type
ParseFloat(s string, bitSize int) (float64, error)
- The first parameter
s
is a string with conversion. - The second parameter is
bitSize
is the converted number of bits,32
representfloat32
,64
representfloat64
。 - The first return value is the converted floating point number.
- The second return value is an error generated during the conversion process
import ( "fmt" "strconv" ) func main() { num, err := ("11.05", 64) if err != nil { return } (num) }
If thes
Syntax errors such as1s1
, the conversion fails and returnserror
。
FormatFloat: Convert floating point number f to string according to format fmt and precision prec
FormatFloat(f float64, fmt byte, prec, bitSize int) string
- The first parameter
f
is the floating point number to be converted. - The second parameter is format, optional values are
b e E f g G x X
。 - The third parameter
prec
For precision, it is accurate to several decimal places. - The return value is the converted string.
import ( "fmt" "strconv" ) func main() { str := (5.26, 'f', 1, 64) (str) // 5.3 }
FormatFloat
The function will round the result value.
Conversion between string and bool
ParseBool: string to boolean
ParseBool(str string) (bool, error)
- The first parameter
str
For the string to be converted - The first return value is after the conversion
bool
value - The second return value is an error generated during conversion.
import ( "fmt" "strconv" ) func main() { bool1, err := ("true") if err != nil { (()) return } (bool1) // true bool2, err := ("golang") if err != nil { (()) // : parsing "golang": invalid syntax return } (bool2) }
The first stringtrue
The Boolean value was successful, but the second stringgolang
The Boolean value failed becausebool
The optional value of type is onlytrue
andfalse
, other values cannot be converted to boolean values.
FormatBool: Boolean to string
FormatBool(b bool) string
- parameter
b
is a Boolean value with conversion. - The return value is the converted string.
import ( "fmt" "strconv" ) func main() { boolStr := (true) (boolStr) // "true" }
summary
This articlestring
andint
type,float
Types andbool
Introduction to functions that convert types to each other, master the usage of these functions, and deal with developmentstring
Scenarios where types are converted to other basic data types are not a problem.
This is the article about the detailed explanation of the use of Go strconv and other basic data type conversion functions. For more related Go strconv content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!