Defragmentation of language go strconv

strconv package

strconv package implements basic data types converted amount of its string representation, mainly in the following common functions, Atoi (), Itia (), parse series, format series, the append series.

string conversion type int

This group of functions that we usually use the most.

Atoi ()

ATiO () function is used to convert the integer string type is an int, the function signature as follows:

func Atoi(s string) (i int, err error)

If the incoming string parameter can not be converted to int type, it will error.

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

Itoa ()

Itoa () function is used to type int into a string of data corresponding to said specific function signature as follows:

func Itoa(i int) string

Sample code is as follows:

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

family of functions parse
Parse function is used to convert the string class to a given type of value: ParseBool (), ParseFloat () , ParseInt (), ParseUnit ().

ParseFloat()

func ParseFloat(s string, bitSize int) (f float64,err error)

Parsing a string of floating-point representation and returns its value.
ParseBool ()

func ParseBool(str string) (value bool,err error)

Returns a string representation of a bool value. It accepts 1,0, t, f, T, F, true, false, True, False, TRUE, FALSE; otherwise it returns an error.

ParseInt()

func ParseInt(s string, base int, bitsize int) (i int64, err error)

Returns a string representation of the integer value, the sign of acceptance.
base specifies a binary (2-36), if the base is 0, from the pre-determined character string, "0x" are hexadecimal "0" is the 8-ary, 10-ary otherwise.
bitSize results must be able to specify the type of non-integer overflow, 0,8,16,32,64 representing int, int8, int16, int32, int64;
return err is * NumErr type, if syntax errors, err.Error = ErrSyntax; If the result is outside the range of types err.Error = ErrRange.

ParseUnit ()

package main
import (
    "strconv"
    "fmt"
)
func main(){
    b , err := strconv.ParseBool("true")
    if err != nil{
        panic(err)
    }
    fmt.Println(b)
    f ,err := strconv.ParseFloat("3.1415",64)
    if err != nil{
        panic(err)
    }
    fmt.Println(f)
    u ,err := strconv.ParseUnit("2",10,64)
    if err != nil{
        panic(err)
    }
    fmt.Println(u)
}

Format function Series

series format function implements the functions of a given type of data formatted as string type data.

FormatBool ()
func FormatBool(b bool) string

Return true or false depending on the value of b.

FormatInt ()
func FormatInt(i int64,base int) string

Return base band i of the string representation. base must be between 2 and 36, the results will lowercase letters "a" to "z" represents a number greater than 10.

FormatUnit ()
func FormatUnit(i int64, base int)

FormatInt is unsigned integer version.

FormatFloat()
func FormatFloat(f float64, fmt byte, prec,bitsize int) string

Function as a string and returns a floating point number.

    s1 := strconv.FormatBool(true)
    s2 := strconv.FormatFloat(3.1415,'E',-1,64)
    s3 := strconv.FormatInt(-2,16)
    // s4 := strconv.FormatUnit(2,16)
    fmt.Println(s1,s2,s3)

Guess you like

Origin blog.51cto.com/13766835/2406540