go语言的魔幻旅程17-strconv包

君问归期未有期,巴山夜雨涨秋池

离家已近一载,虽偶有思乡欲踏归途之心,但奈何各种各样现实原因的羁绊,始终未能如愿动身,或许下一次的归途似乎只能安排到下一个收获的黄金时节。说起家乡对于大多数人而言总感觉是一个神秘的地方,在的时候总想着逃离,离开时候总想着回归,果然人类的悲欢离合总是那么的惊奇。

go语言的strconv包

go语言的strconv包主要提供了一些不同数据类型相互转化的函数和方法,下面将针对这些函数方法展开讲解。

package main

/*********************************************************************/
/**************** golang中strconv包相关API讲解 *********************/
/********************************************************************/

/*
Constants
Variables
func AppendBool(dst []byte, b bool) []byte
func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte
func AppendInt(dst []byte, i int64, base int) []byte
func AppendQuote(dst []byte, s string) []byte
func AppendQuoteRune(dst []byte, r rune) []byte
func AppendQuoteRuneToASCII(dst []byte, r rune) []byte
func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte
func AppendQuoteToASCII(dst []byte, s string) []byte
func AppendQuoteToGraphic(dst []byte, s string) []byte
func AppendUint(dst []byte, i uint64, base int) []byte
func Atoi(s string) (int, error)
func CanBackquote(s string) bool
func FormatBool(b bool) string
func FormatComplex(c complex128, fmt byte, prec, bitSize int) string
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
func FormatInt(i int64, base int) string
func FormatUint(i uint64, base int) string
func IsGraphic(r rune) bool
func IsPrint(r rune) bool
func Itoa(i int) string
func ParseBool(str string) (bool, error)
func ParseComplex(s string, bitSize int) (complex128, error)
func ParseFloat(s string, bitSize int) (float64, error)
func ParseInt(s string, base int, bitSize int) (i int64, err error)
func ParseUint(s string, base int, bitSize int) (uint64, error)
func Quote(s string) string
func QuoteRune(r rune) string
func QuoteRuneToASCII(r rune) string
func QuoteRuneToGraphic(r rune) string
func QuoteToASCII(s string) string
func QuoteToGraphic(s string) string
func Unquote(s string) (string, error)
func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
type NumError
	func (e *NumError) Error() string
	func (e *NumError) Unwrap() error
*/

func main() {

	/**
	*AppendBool根据b的值将"true"或"false"附加到dst并返回扩展缓冲区。
	*func AppendBool(dst []byte, b bool) []byte
	*/

	/*
	b := []byte("bool:")
	b = strconv.AppendBool(b, true)
	fmt.Println(string(b))
	*/

	/**
	*AppendFloat将由FormatFloat生成的浮点数f的字符串形式附加到dst,并返回扩展缓冲区。
	*func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte
	*/

	/*
	b32 := []byte("float32:")
	b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
	fmt.Println(string(b32))

	b64 := []byte("float64:")
	b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
	fmt.Println(string(b64))
	*/

	/**
	*AppendInt将由FormatInt生成的整数i的字符串形式附加到dst,并返回扩展缓冲区。
	*func AppendInt(dst []byte, i int64, base int) []byte
	*/

	/*
	b10 := []byte("int (base 10):")
	b10 = strconv.AppendInt(b10, -42, 10)
	fmt.Println(string(b10))

	b16 := []byte("int (base 16):")
	b16 = strconv.AppendInt(b16, -42, 16)
	fmt.Println(string(b16))
	*/

	/**
	*AppendQuote将由Quote生成的表示s的double-quoted Go字符串文字附加到dst,
	*并返回扩展缓冲区。
	*func AppendQuote(dst []byte, s string) []byte
	*/

	/*
	b := []byte("quote:")
	b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
	fmt.Println(string(b))
	*/

	/**
	*AppendQuoteRune将由QuoteRune生成的代表符文的single-quoted Go字符文字附加到dst,
	*并返回扩展缓冲区。
	*func AppendQuoteRune(dst []byte, r rune) []byte
	*/

	/*
	b := []byte("rune:")
	b = strconv.AppendQuoteRune(b, '☺')
	fmt.Println(string(b))
	*/

	/**
	*AppendQuoteToASCII将QuoteToASCII生成的表示s的double-quoted Go字符串文字附加到dst,
	*并返回扩展缓冲区。
	*func AppendQuoteRuneToASCII(dst []byte, r rune) []byte
	*/

	/*
	b := []byte("quote (ascii):")
	b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
	fmt.Println(string(b))
	*/

	/**
	*QuoteToGraphic返回代表s的double-quoted Go字符串文字。返回的字符串保留IsGraphic定义的
	*Unicode图形字符不变,并且对非图形字符使用Go转义序列(\ t,\ n,\ xFF,\ u0100)。
	*func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte
	*/

	/*
	s := strconv.QuoteToGraphic("☺")
	fmt.Println(s)

	s = strconv.QuoteToGraphic("This is a \u263a	\u000a")
	fmt.Println(s)

	s = strconv.QuoteToGraphic(`" This is a ☺ \n "`)
	fmt.Println(s)
	*/


	/**
	*AppendQuoteToASCII将QuoteToASCII生成的表示s的double-quoted Go字符串文字附加到dst,
	*并返回扩展缓冲区。
	*func AppendQuoteToASCII(dst []byte, s string) []byte
	*/

	/*
	b := []byte("quote (ascii):")
	b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
	fmt.Println(string(b))
	*/

	/**
	*
	*func AppendQuoteToGraphic(dst []byte, s string) []byte
	*/


	/**
	*AppendUint将由FormatUint生成的无符号整数i的字符串形式附加到dst,然后返回扩展缓冲区。
	*func AppendUint(dst []byte, i uint64, base int) []byte
	*/

	/*
	b10 := []byte("uint (base 10):")
	b10 = strconv.AppendUint(b10, 42, 10)
	fmt.Println(string(b10))

	b16 := []byte("uint (base 16):")
	b16 = strconv.AppendUint(b16, 42, 16)
	fmt.Println(string(b16))
	*/


	/**
	*AppendUint将由FormatUint生成的无符号整数i的字符串形式附加到dst,然后返回扩展缓冲区。
	*func Atoi(s string) (int, error)
	*/

	/*
	v := "10"
	if s, err := strconv.Atoi(v); err == nil {
		fmt.Printf("%T, %v", s, s)
	}
	*/

	/**
	*CanBackquote报告字符串s是否可以不变地表示为单行反引号字符串,并且没有制表符以外的控制字符。
	*func CanBackquote(s string) bool
	*/

	/*
	fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺"))
	fmt.Println(strconv.CanBackquote("`can't backquote this`"))
	*/

	/**
	*FormatBool根据b的值返回"true"或"false"。
	*func FormatBool(b bool) string
	*/

	/*
	v := true
	s := strconv.FormatBool(v)
	fmt.Printf("%T, %v\n", s, s)
	*/

	/**
	*
	*func FormatComplex(c complex128, fmt byte, prec, bitSize int) string
	*/


	/**
	*根据设定的进度转换为指定的float类型
	*func FormatFloat(f float64, fmt byte, prec, bitSize int) string
	*/

	/*
	v := 3.1415926535
	s32 := strconv.FormatFloat(v, 'E', -1, 32)
	fmt.Printf("%T, %v\n", s32, s32)

	s64 := strconv.FormatFloat(v, 'E', -1, 64)
	fmt.Printf("%T, %v\n", s64, s64)
	*/

	/**
	*FormatInt以2 <= base <= 36的形式返回给定基数i的字符串表示形式。
	*结果使用小写字母'a'到'z'表示数字值> = 10。
	*func FormatInt(i int64, base int) string
	*/

	/*
	v := int64(-42)
	s10 := strconv.FormatInt(v, 10)
	fmt.Printf("%T, %v\n", s10, s10)
	s16 := strconv.FormatInt(v, 16)
	fmt.Printf("%T, %v\n", s16, s16)
	*/

	/**
	*IsGraphic报告是否通过Unicode将符文定义为图形。此类字符包括字母,标记,
	*数字,标点符号,符号和空格,来自类别L,M,N,P,S和Zs。
	*func IsGraphic(r rune) bool
	*/

	/*
	shamrock := strconv.IsGraphic('☘')
	fmt.Println(shamrock)

	a := strconv.IsGraphic('a')
	fmt.Println(a)

	bel := strconv.IsGraphic('\007')
	fmt.Println(bel)
	*/

	/**
	*IsPrint报告符文是否已定义为Go可打印的符文,其定义与unicode.IsPrint:
	*字母,数字,标点,符号和ASCII空间。
	*func IsPrint(r rune) bool
	*/

	/*
	c := strconv.IsPrint('\u263a')
	fmt.Println(c)

	bel := strconv.IsPrint('\007')
	fmt.Println(bel)
	*/

	/**
	*Itoa等效于FormatInt(int64(i),10)。
	*func Itoa(i int) string
	*/

	/*
	i := 10
	s := strconv.Itoa(i)
	fmt.Printf("%T, %v\n", s, s)
	*/

	/**
	*ParseBool返回由字符串表示的布尔值。它接受1,t,T,TRUE,true,True,0,f,F,FALSE,
	*false,False。其他任何值都将返回错误。
	*func ParseBool(str string) (bool, error)
	*/

	/*
	v := "true"
	if s, err := strconv.ParseBool(v); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	*/

	/**
	*
	*func ParseComplex(s string, bitSize int) (complex128, error)
	*/

	/**
	*ParseFloat将字符串s转换为由bitSize指定的精度的浮点数
	*func ParseFloat(s string, bitSize int) (float64, error)
	*/

	/*
	v := "3.1415926535"
	if s, err := strconv.ParseFloat(v, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat(v, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("NaN", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("nan", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("-0", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseFloat("+0", 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	*/

	/**
	*ParseInt解析给定基数(0、2到36)和位大小(0到64)中的字符串s,并返回相应的值i。
	*func ParseInt(s string, base int, bitSize int) (i int64, err error)
	*/

	/*
	v32 := "-354634382"
	if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}

	v64 := "-3546343826724305832"
	if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	*/

	/**
	*ParseUint类似于ParseInt,但用于无符号数字。
	*func ParseUint(s string, base int, bitSize int) (uint64, error)
	*/

	/*
	v := "42"
	if s, err := strconv.ParseUint(v, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	if s, err := strconv.ParseUint(v, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)
	}
	*/

	/**
	*引用返回一个表示s的double-quoted Go字符串文字。返回的字符串使用Go转义序列
	*(\ t,\ n,\ xFF,\ u0100)来控制字符和IsPrint定义的不可打印字符。
	*func Quote(s string) string
	*/

	/*
	s := strconv.Quote(`"Fran & Freddie's Diner	☺"`)
	fmt.Println(s)
	*/

	/**
	*QuoteRune返回代表符文的single-quoted Go字符文字。返回的字符串使用Go转义序列
	*(\ t,\ n,\ xFF,\ u0100)来控制字符和IsPrint定义的不可打印字符。
	*func QuoteRune(r rune) string
	*/

	/*
	s := strconv.QuoteRune('☺')
	fmt.Println(s)
	*/

	/**
	*QuoteRuneToASCII返回代表符文的single-quoted Go字符文字。返回的字符串对IsASCII
	*定义的非ASCII字符和不可打印的字符使用Go转义序列(\ t,\ n,\ xFF,\ u0100)。
	*func QuoteRuneToASCII(r rune) string
	*/

	/*
	s := strconv.QuoteRuneToASCII('☺')
	fmt.Println(s)
	*/

	/**
	*QuoteRuneToGraphic返回代表符文的single-quoted Go字符文字。如果符文不是IsGraphic
	*定义的Unicode图形字符,则返回的字符串将使用Go转义序列(\ t,\ n,\ xFF,\ u0100)。
	*func QuoteRuneToGraphic(r rune) string
	*/

	/*
	s := strconv.QuoteRuneToGraphic('☺')
	fmt.Println(s)

	s = strconv.QuoteRuneToGraphic('\u263a')
	fmt.Println(s)

	s = strconv.QuoteRuneToGraphic('\u000a')
	fmt.Println(s)

	s = strconv.QuoteRuneToGraphic('	') // tab character
	fmt.Println(s)
	*/

	/**
	*QuoteToASCII返回代表s的double-quoted Go字符串文字。返回的字符串对
	*IsASCII定义的非ASCII字符和不可打印的字符使用Go转义序列(\ t,\ n,\ xFF,\ u0100)。
	*func QuoteToASCII(s string) string
	*/

	/*
	s := strconv.QuoteToASCII(`"Fran & Freddie's Diner	☺"`)
	fmt.Println(s)
	*/

	/**
	*QuoteToGraphic返回代表s的double-quoted Go字符串文字。返回的字符串保留IsGraphic定义
	*的Unicode图形字符不变,并且对非图形字符使用Go转义序列(\ t,\ n,\ xFF,\ u0100)。
	*func QuoteToGraphic(s string) string
	*/

	/*
	s := strconv.QuoteToGraphic("☺")
	fmt.Println(s)
	s = strconv.QuoteToGraphic("This is a \u263a	\u000a")
	fmt.Println(s)
	s = strconv.QuoteToGraphic(`" This is a ☺ \n "`)
	fmt.Println(s)
	*/

	/**
	*Unquote将s解释为single-quoted,double-quoted或带反引号的Go字符串文字,
        *并返回用s引起引用的字符串值。
	*(如果s是single-quoted,则它将是Go字符文字; Unquote返回相应的one-character字符串。)
	*func Unquote(s string) (string, error)
	*/

	/*
	s, err := strconv.Unquote("You can't unquote a string without quotes")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("\"The string must be either double-quoted\"")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("`or backquoted.`")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("'\u263a'") // single character only allowed in single quotes
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.Unquote("'\u2639\u2639'")
	fmt.Printf("%q, %v\n", s, err)
	*/

	/**
	*UnquoteChar解码转义的字符串或由字符串s表示的字符文字中的第一个字符或字节。
	*它返回四个值
	*func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
	*/

	/*
	v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("value:", string(v))
	fmt.Println("multibyte:", mb)
	fmt.Println("tail:", t)
	*/

	/**********************************************************************/
	/**************** NumError中相关方法讲解 ***************************/
	/********************************************************************/

	/**
	*NumError记录转换失败
	*func (e *NumError) Error() string
	*/

	/*
	str := "Not a number"
	if _, err := strconv.ParseFloat(str, 64); err != nil {
		e := err.(*strconv.NumError)
		fmt.Println("Func:", e.Func)
		fmt.Println("Num:", e.Num)
		fmt.Println("Err:", e.Err)
		fmt.Println(err)
	}
	*/


	/**
	*使用 Unwrap 方法可以获取一个错误中包含的另外一个错误
	*func (e *NumError) Unwrap() error
	*/

}

小结

strconv这部分的内容讲解的内容虽然很多,但是实际上常用的转换的类型也就是几种,从实际的掌握难度上来讲应该不是非常的困难,因此这个部分的内容还是强力建议必须拿下来。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值