strconv of Go language built-in package

1.strconv package

The strconv package implements the conversion of basic data types and their string representation, mainly including the following common functions: atoi(), ita(), parse series, format series, append series.

1.1 string and int type conversion

1.1.1Atoi()

The Atoi() function is used to convert an integer of type string to type int
func Atoi(s string) (i int, err error)
If the passed in string parameter cannot be converted to Int type, an error is returned.
package main

import (
    "fmt"
    "strconv"
)

func main() {
    s1 := "100"
    i1,err := strconv.Atoi(s1)
    if err != nil{
        fmt.Println("can not convert to int")
    }else{
        fmt.Printf("type:%T value:%#v\n",i1,i1)
    }
}

//Result:
type:int value:100

Process finished with exit code 0

1.1.2Itoa()

Itoa()Function is used to int The core data is converted to the corresponding string representation.
func Itoa(i int) string
package main

import (
    "fmt"
    "strconv"
)

func main() {
    i1 := 100
    s1 := strconv.Itoa(i1)
    fmt.Printf("type:%T value:%#v\n",s1,s1)
}

//Result:
type:string value:"100"

Process finished with exit code 0
a's allusion:
[extended reading] this is the allusion left by C language. There is no string type in C language, but character array is used to represent string, so Itoa has a good understanding of many C system programmers.

1.2 parse series functions

Parse series functions are used to convert strings to values of the given type: parsebool(), parsefloat(), parseint().

1.2.1ParseBool()

func ParseBool(str string) (value bool, err error)
Returns the bool value represented by a string. It receives 1,0,t,f,T,F,true,True,False,TRUE,FALSE; otherwise, it returns an error.

1.2.2ParseInt()

func ParseInt(s string, base int, bitSize int) (i int64, err error)
Returns the integer value represented by a string, and accepts a plus or minus sign.
Base: Specifies the base (2 to 36). If base is 0, it will be judged from the string's front end, "0x" is hexadecimal, "0" is octal, otherwise it is decimal;
bitSize: Specifies that the result must be an integer type without overflow assignment. 0,8,16,32,64 represent int,int8,int16,int32,int64 respectively;
Err: it is of type * NumErr. If there is a syntax error, err.Error=ErrSyntax; if it is out of the type range, err.Error=ErrRange.

1.2.3ParseUint()

func ParseUint(s string, base int, bitSize int) (n uint64, err error)
ParseUint Similar ParseInt Signs are not accepted for unsigned integers.

1.2.4ParseFloat()

func ParseFloat(s string, bitSize int) (f float64, err error)
Resolves a string representing a floating-point number and returns its value.
If s is syntactically correct, the function returns a floating-point number (rounded using the IEEE754 specification) closest to the value of S.
bitSize: Specifies the expected receive type, 32 is float32 (the return value can be assigned to float32 without changing the exact value), 64 is float64;
Err: it is of type * NumErr with incorrect syntax, err.Error=ErrSyntax; if the result is out of the range, the return value f is ± Inf, err.Error= ErrRange.

1.2.5 example

package main

import (
    "fmt"
    "strconv"
)

func main() {
    b, _ := strconv.ParseBool("true")
    fmt.Println(b)
    f, _ := strconv.ParseFloat("3.1415", 64)
    fmt.Println(f)
    i, _ := strconv.ParseInt("-2", 10, 64)
    fmt.Println(i)
    u, _ := strconv.ParseUint("2", 10, 64)
    fmt.Println(u)
}

//Result:
true
3.1415
-2
2

Process finished with exit code 0

1.3 format series functions

The Format series functions realize the function of formatting the given type data into string type data.

1.3.1FormatBool()

func FormatBool(b bool) string
 Returns "true" or "false" based on the value of b.

1.3.2FormatInt()

func FormatInt(i int64, base int) string
 Returns the base string representation of i. Base must be between 2 and 36, and lowercase letters' a 'to' z 'will be used in the result for numbers greater than 10.

1.3.3FormatUint()

func FormatUint(i uint64, base int) string
 Is the unsigned integer version of FormatInt.

1.3.4FormatFloat()

func FormatFloat(f float64, fmt byte, prec, bitSize int) string
bitSize Express f Source type of (32: float32,64: float64),It will be rounded accordingly.

fmt Presentation format:'f'(-ddd.dddd),'b'(-ddddp±ddd,Index is binary)'e'(-d.dddde±dd,Decimal index)'E'(-d.ddddE±dd,Decimal index)'g'(When the index is large'e'Format, otherwise'f'Format)'G'(When the index is large'E'Format, otherwise'f'Format)

prec Control accuracy (excluding index part): Yes'f','e','E',It represents the number of digits after the decimal point; right'g','G',It controls the total number of digits. If prec by-1,The minimum number of necessary numbers f. 

1.3.5 example

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s1 := strconv.FormatBool(true)
    s2 := strconv.FormatFloat(3.1415, 'E', -1, 64)
    s3 := strconv.FormatInt(-2, 16)
    s4 := strconv.FormatUint(2, 16)
    fmt.Printf("Type:%T value:%#v\n",s1,s1)
    fmt.Printf("Type:%T value:%#v\n",s2,s2)
    fmt.Printf("Type:%T value:%#v\n",s3,s3)
    fmt.Printf("Type:%T value:%#v\n",s4,s4)
}

//Result:
Type:string value:"true"
Type:string value:"3.1415E+00"
Type:string value:"-2"
Type:string value:"2"

Process finished with exit code 0

1.4 other

1.4.1isPrint()

func IsPrint(r rune) bool
 Returns whether a character is printable. Like unicode.IsPrint, r must be: letter (generalized), number, punctuation, symbol, ASCII space.

1.4.2CanBackquote()

func CanBackquote(s string) bool
 Returns whether the string s can be unmodified as a single line backquote string without spaces and control characters other than tab.

Other uses can be found in official documents: https://golang.org/pkg/strconv/

Tags: Go C ascii

Posted on Fri, 07 Feb 2020 06:18:32 -0500 by AVATAr