Package strconv
Overview ▾
Package strconv implements conversions to and from string representations of basic data types. strconv包实现从字符串转换成基础数据类型.
Constants
const IntSize = intSize
IntSize is the size in bits of an int or uint value. IntSize是 int 或uint 值的位 大小.
Variables
var ErrRange = errors.New("value out of range")
ErrRange indicates that a value is out of range for the target type. ErrRange说明值超出目标类型范围.
var ErrSyntax = errors.New("invalid syntax")
ErrSyntax indicates that a value does not have the right syntax for the target type. ErrSyntax说明 值 没有正确的目标类型语法
func AppendBool
func AppendBool(dst []byte, b bool) []byte
AppendBool appends "true" or "false", according to the value of b, to dst and returns the extended buffer. AppendBool 根据值b 追加 "true" 或 "false" 到dst 并返回扩展的缓冲区
func AppendFloat
func AppendFloat(dst []byte, f float64, fmt byte, prec int, bitSize int) []byte
AppendFloat appends the string form of the floating-point number f, as generated by FormatFloat, to dst and returns the extended buffer. AppendFloat 追加根据 FormatFloat 产生的字符串形式的 浮点型数f 到 dst 并返回扩展的缓冲区.
func AppendInt
func AppendInt(dst []byte, i int64, base int) []byte
AppendInt appends the string form of the integer i, as generated by FormatInt, to dst and returns the extended buffer. AppendInt 追加 根据FormatInt 产生字符串形式 整型i 到dst 并返回扩展的缓冲区.
func AppendQuote
func AppendQuote(dst []byte, s string) []byte
AppendQuote appends a double-quoted Go string literal representing s, as generated by Quote, to dst and returns the extended buffer. AppendQuote 追加根据 Quote产生 双引号GO 字符串字面表示s 到dst 并返回扩展的缓冲区.
func AppendQuoteRune
func AppendQuoteRune(dst []byte, r rune) []byte
AppendQuoteRune appends a single-quoted Go character literal representing the rune, as generated by QuoteRune, to dst and returns the extended buffer. AppendQuoteRune 追加根据 QuoteRune 生成 单引号GO 子符字面 表示 rune 到dst 并返回扩展的缓冲区.
func AppendQuoteRuneToASCII
func AppendQuoteRuneToASCII(dst []byte, r rune) []byte
AppendQuoteRuneToASCII appends a single-quoted Go character literal representing the rune, as generated by QuoteRuneToASCII, to dst and returns the extended buffer. AppendQuoteRuneToASCII 追加根据 QuoteRuneToASCII 生成 单引号GO 子符字面 表示 rune 到dst 并返回扩展的缓冲区.
func AppendQuoteToASCII
func AppendQuoteToASCII(dst []byte, s string) []byte
AppendQuoteToASCII appends a double-quoted Go string literal representing s, as generated by QuoteToASCII, to dst and returns the extended buffer. AppendQuoteToASCII追加根QuoteToASCII 生成 双引号GO 子符字面 表示 s 到dst 并返回扩展的缓冲区.
func AppendUint
func AppendUint(dst []byte, i uint64, base int) []byte
AppendUint appends the string form of the unsigned integer i, as generated by FormatUint, to dst and returns the extended buffer. AppendUint追加根据 FormatUint 产生 字符串形式 无符号整型i 到dst 并返回扩展的缓冲区.
func Atoi
func Atoi(s string) (i int, err error)
Atoi is shorthand for ParseInt(s, 10, 0). Atoi 是 ParseInt(s, 10, 0)的简写
func CanBackquote
func CanBackquote(s string) bool
CanBackquote reports whether the string s can be represented unchanged as a single-line backquoted string without control characters other than space and tab. CanBackquote 报告字符串s 是否可以表示 不变的 没有空格和值表符等控制字符 的 单行反引号字符串
func FormatBool
func FormatBool(b bool) string
FormatBool returns "true" or "false" according to the value of b FormatBool 根据值b 返回 "true" 或 "false" .
func FormatFloat
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).
The format fmt is one of 'b' (-ddddp±ddd, a binary exponent), 'e' (-d.dddde±dd, a decimal exponent), 'E' (-d.ddddE±dd, a decimal exponent), 'f' (-ddd.dddd, no exponent), 'g' ('e' for large exponents, 'f' otherwise), or 'G' ('E' for large exponents, 'f' otherwise).
The precision prec controls the number of digits (excluding the exponent) printed by the 'e', 'E', 'f', 'g', and 'G' formats. For 'e', 'E', and 'f' it is the number of digits after the decimal point. For 'g' and 'G' it is the total number of digits. The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly. FormatFloat转换 浮点型 数 f 成一个字符串, 根据fmt格式和prec精度. 它四舍五入结果 假定源 从 浮点值bitSize位 获取(32 for float32, 64 for float64).
fmt格式是 'b' (-ddddp±ddd, a binary exponent), 'e' (-d.dddde±dd, a decimal exponent), 'E' (-d.ddddE±dd, a decimal exponent), 'f' (-ddd.dddd, no exponent), 'g' ('e' for large exponents, 'f' otherwise), or 'G' ('E' for large exponents, 'f' otherwise)中的一个
精度 prec 通过 'e', 'E', 'f', 'g', and 'G'格式 控制打印的位数. 对于 'e', 'E', and 'f' 它是小数点之后的位数.对于 'g' and 'G' 它是总位数。 特别的精度-1 使用最少的位, 这样ParseFloat 将恰好返回f.
func FormatInt
func FormatInt(i int64, base int) string
FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10. FormatInt 在给定的base 里 返回字符串 表示i,2 <= base <= 36. 结果 values >= 10 使用小写字母 'a' 到 'z'.
func FormatUint
func FormatUint(i uint64, base int) string
FormatUint returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10. FormatUint 在给定的base 里 返回字符串 表示i,2 <= base <= 36. 结果 values >= 10 使用小写字母 'a' 到 'z'.
func IsPrint
func IsPrint(r rune) bool
IsPrint reports whether the rune is defined as printable by Go, with the same definition as unicode.IsPrint: letters, numbers, punctuation, symbols and ASCII space. IsPrint 报告 rune 是否 根据GO 打印,具有相同的定义如unicode.IsPrint:letters, numbers, punctuation, symbols and ASCII space.
func Itoa
func Itoa(i int) string
Itoa is shorthand for FormatInt(i, 10). Itoa是FormatInt(i, 10)简写.
func ParseBool
func ParseBool(str string) (value bool, err error)
ParseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error. ParseBool 返回string的 布尔值表示. 它接收 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. 任何其他值返回错误.
func ParseFloat
func ParseFloat(s string, bitSize int) (f float64, err error)
ParseFloat converts the string s to a floating-point number with the precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value.
If s is well-formed and near a valid floating point number, ParseFloat returns the nearest floating point number rounded using IEEE754 unbiased rounding.
The errors that ParseFloat returns have concrete type *NumError and include err.Num = s.
If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
If s is syntactically well-formed but is more than 1/2 ULP away from the largest floating point number of the given size, ParseFloat returns f = ±Inf, err.Err = ErrRange.
ParseFloat 通过指定的bitSize 精度 转换字符串s成一个浮点型数:32 for float32, or 64 for float64.当bitSize=32时,结果依旧是float64类型,但是它将转换为float32 不修改它的值. 如果s是良好的 并近一个有效的浮点数,ParseFloat 返回使用 IEEE754无偏四舍五入 ,四舍五入最近的 浮点型 .
ParseFloat 的 errors 返回具体*NumError 类型 并包含 err.Num = s.
如果s 不是语法结构良好的,ParseFloat 返回 err.Err = ErrSyntax.
如果s 是语法结构良好的 但是超过 1/2 ULP 离给定尺寸的最大浮点数,ParseFloat返回 f = ±Inf, err.Err = ErrRange.
func ParseInt
func ParseInt(s string, base int, bitSize int) (i int64, err error)
ParseInt interprets a string s in the given base (2 to 36) and returns the corresponding value i. If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.
The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.
The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax and the returned value is 0; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign.
ParseInt 在给定的 base(2 to 36) 解 字符串s 并返回对应的值i. 如果base == 0, base 隐含的字符串的前缀:base 16 对 "0x",base 8 对 "0", 否则 base 10 .
bitSize参数指定 整型类型 结果必须填充进取.位大小 0, 8, 16, 32, and 64 对应 int, int8, int16, int32, and int64.
error ParseInt 返回具体类型 *NumError 并包含 err.Num = s. 如果s是空的或包含无效的位,err.Err = ErrSyntax 并 返回值是0. 如果 值对应s 不能通过 给定大小的单个整型表示,err.Err = ErrRange 并 返回值是适当的bitSize和符号的最大量值的整数。
func ParseUint
func ParseUint(s string, base int, bitSize int) (n uint64, err error)
ParseUint is like ParseInt but for unsigned numbers. ParseUint 类似ParseInt 但是 无符号数.
func Quote
func Quote(s string) string
Quote returns a double-quoted Go string literal representing s. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for control characters and non-printable characters as defined by IsPrint. Quote 返回一个双引号GO字符串文本 表示 s. 返回字符串 使用GO过滤(\t, \n, \xFF, \u0100) 控制子符序列 和 通过IsPrint定义的非打印子符
func QuoteRune
func QuoteRune(r rune) string
QuoteRune returns a single-quoted Go character literal representing the rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for control characters and non-printable characters as defined by IsPrint. QuoteRune 返回一个单引号GO字符串文本 表示 rune. 返回字符串 使用GO过滤(\t, \n, \xFF, \u0100) 控制子符序列 和 通过IsPrint定义的非打印子符
func QuoteRuneToASCII
func QuoteRuneToASCII(r rune) string
QuoteRuneToASCII returns a single-quoted Go character literal representing the rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for non-ASCII characters and non-printable characters as defined by IsPrint. QuoteRuneToASCII 返回一个单引号GO字符串文本 表示 rune. 返回字符串 使用GO过滤(\t, \n, \xFF, \u0100) 控制子符序列 和 通过IsPrint定义的非打印子符
func QuoteToASCII
func QuoteToASCII(s string) string
QuoteToASCII returns a double-quoted Go string literal representing s. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for non-ASCII characters and non-printable characters as defined by IsPrint. QuoteToASCII 返回一个双引号GO字符串文本 表示 s. 返回字符串 使用GO过滤(\t, \n, \xFF, \u0100) 控制子符序列 和 通过IsPrint定义的非打印子符
func Unquote
func Unquote(s string) (t string, err error)
Unquote interprets s as a single-quoted, double-quoted, or backquoted Go string literal, returning the string value that s quotes. (If s is single-quoted, it would be a Go character literal; Unquote returns the corresponding one-character string.) Unquote解 一个单引号,双引号, 或反引号GO字符串文本,返回的字符串值是s引号。(如果s是一个单引号,它将是GO字符文本;Unquote 返回 对应的单字符 字符串 )
▹ Example
func UnquoteChar
func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
UnquoteChar decodes the first character or byte in the escaped string or character literal represented by the string s. It returns four values: UnquoteChar解码 第一个字符或字节 在转义字符串或字符文字 的字符串s 表示.它返回四个值:
1) value, the decoded Unicode code point or byte value;
2) multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation;
3) tail, the remainder of the string after the character; and
4) an error that will be nil if the character is syntactically valid.
1)value,解码 Unicode 或 字节值;
2)multibyte,一个布尔值,表示解码字符是否需要多字节UTF-8表示;
3)tail,该字符后面的剩余部分的字符串;
4)如果字符语法有效, 错误将是 nil.
The second argument, quote, specifies the type of literal being parsed and therefore which escaped quote character is permitted. If set to a single quote, it permits the sequence ' and disallows unescaped '. If set to a double quote, it permits " and disallows unescaped ". If set to zero, it does not permit either escape and allows both quote characters to appear unescaped. 第二个参数quote 指定文本解析的类型 ,因此,它过滤引号字符是允许的。 如果设置成单引号,它允许 序列' 和不允许 '. 如果设置成双引号,它允许" 和不允许 ". 如果设置成零,它不允许 过滤 但并允许两个引号字符出现转义。
type NumError
type NumError struct {
Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat)
Num string // the input
Err error // the reason the conversion failed (ErrRange, ErrSyntax)
}
A NumError records a failed conversion. NumError 返回一个失败说明.
func (*NumError) Error
func (e *NumError) Error() string