strconv package
This package mainly implements the conversion of basic data types and their string representations.
Common functions areAtoi()
、Itia()
、parse
series,format
series,append
series.
For more functions, please checkOfficial Documentation。
string and int type conversion
Atoi()
This function is used to convert integers of string type toint
Type, function signature is as follows:
func Atoi(s string) (i int, err error)
If the passed string parameter cannot be converted to int type, an error will be returned.
Sample demonstration:
package main import ( "fmt" "strconv" ) func main(){ strInt := "100" num,err := (strInt) if err != nil{ ("can't convert to int",err) return } ("type:%T\nvalue:%#v\n",num,num) // type:int // value:100 }
Itoa()
This function is used toint
Type data is converted into the corresponding string representation, and the function signature is as follows:
func Itoa(i int) string
The sample code is as follows:
package main import ( "fmt" "strconv" ) func main(){ num := 100 strInt := (num) ("type:%T\nvalue:%#v\n",strInt,strInt) // type:string // value:"100" }
Why not s
You can notice, no matter whatint
changestring
stillstring
changeint
, all usea
Represents a string, usei
representint
Type of data. Why not use its
Woolen cloth? This is actuallyC
No in the languagestring
Type reasons,c
In language, strings are represented by arrays of characters. So the above two functions usea
to represent a string, that isarray
The first letter of
Parse Series
Parse
Class functions are used to convert strings to values of a given type:ParseBool()
、ParseFloat()
、ParseInt()
、ParseUint()
。
ParseBool()
func ParseBool(str string) (value bool, err error)
Returns the string representationbool
value. It only accepts 1, 0, t, f, T, F, true, false, True, False, TRUE, FALSE; otherwise, an error is returned.
package main import ( "fmt" "strconv" ) func main() { result, _ := ("1") ("type:%T\n value:%#v\n", result, result) // type:bool // value:true }
ParseInt()
func ParseInt(s string, base int, bitSize int) (i int64, err error)
Returns the integer value represented by the string, accepting a positive and negative sign.
base
Specify the binary (2 to 36), ifbase
If it is 0, it will be judged from the pre-standard, "0x" is hexadecimal, "0" is octal, otherwise it is decimal;
bitSize
Specifies that the integer type that the result must be able to be assigned without overflow. 0, 8, 16, 32, 64 represents int, int8, int16, int32, and int64, respectively;
Returnederr
yes*NumErr
Type, if syntax is wrong, = ErrSyntax; if the result is outside the type range = ErrRange.
package main import ( "fmt" "strconv" ) func main() { // Return 10, decimal, the result is int64 result, _ := ("127", 0, 64) ("type:%T\n value:%#v\n", result, result) // type:int64 // value:127 }
ParseUnit()
func ParseUint(s string, base int, bitSize int) (n uint64, err error)
ParseUint
similarParseInt
However, it does not accept positive and negative signs and is used for unsigned integers.
ParseFloat()
func ParseFloat(s string, bitSize int) (f float64, err error)
Parses a string representing a floating point number and returns its value.
If s complies with syntax rules, the function returns a floating point number closest to the value of s (rounded using the IEEE754 specification).
bitSize specifies the expected reception type, 32 is float32 (the return value can be assigned to float32 without changing the exact value), and 64 is float64;
The return value err is of type *NumErr, if the syntax is incorrect, =ErrSyntax; if the result is beyond the representation range, the return value f is ±Inf, = ErrRange.
package main import ( "fmt" "strconv" ) func main() { result, _ := ("3.1415", 64) ("type:%T\n value:%#v\n", result, result) // type:float64 // value:3.1415 }
Format series
Format
Series functions implement formatting the given type of data intostring
Functions of type data.
FormatBool()
func FormatBool(b bool) string
Return according to the value of btrue
orfalse
。
package main import ( "fmt" "strconv" ) func main(){ result := (true) ("type:%T\n value:%#v\n",result,result) // type:string // value:"true" }
FormatInt()
func FormatInt(i int64, base int) string
Return to ibase
The string representation of the binary system.base
It must be between 2 and 36, and the lowercase letters 'a' to 'z' will be used to represent numbers greater than 10.
package main import ( "fmt" "strconv" ) func main() { result := (127, 2) // Binary string ("type:%T\n value:%#v\n", result, result) // type:string // value:"1111111" }
FormatUint()
func FormatUint(i uint64, base int) string
Same as above, but negative numbers are not supported.
yesFormatInt()
unsigned integer version of .
FormatFloat()
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
The function represents a floating point number as a string and returns.
bitSize represents the source type of f (32: float32, 64: float64), and will be rounded accordingly.
fmt represents formats: 'f' (-), 'b' (-dddp±ddd, exponent is binary), 'e' (-±dd, decimal index), 'E' (-±dd, decimal index), 'g' (used in 'e' format when the exponent is large, otherwise 'f' format), 'G' (used in 'E' format when the exponent is large, otherwise 'f' format).
prec controls accuracy (excluding exponential part): for 'f', 'e', 'E', it represents the number of numbers after the decimal point; for 'g', 'G', it controls the total number of numbers. If prec is -1, it means that f is represented by the minimum number of but necessary numbers.
package main import ( "fmt" "strconv" ) func main() { result := (3.1415, 'e', 2, 32) // Note that the second parameter is a character, not a string. Single quotes ("type:%T\n value:%#v\n", result, result) // type:string // value:"3.14e+00" }
This is all about this article about detailed explanation of Go strconv package. For more related Go strconv package content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!