📌  相关文章
📜  Golang 中的 strconv 包

📅  最后修改于: 2021-10-24 14:05:03             🧑  作者: Mango

Go 语言提供了一个 strconv 包,它实现了与基本数据类型的字符串表示之间的转换。要访问 strconv 包的功能,您需要借助 import 关键字在程序中导入 strconv 包。

Function Description
func AppendBool This function is used to appends true or false according to the value of x, to dst and returns the extended buffer.
func AppendFloat This function is used to appends the string form of the floating-point number f, as generated by FormatFloat, to dst and returns the extended buffer.
func AppendInt This function is used to appends the string form of the integer i, as generated by FormatInt, to dst and returns the extended buffer.
func AppendQuote This function is used to appends a double-quoted Go string literal representing s, as generated by Quote, to dst and returns the extended buffer.
func AppendQuoteRune This function is used to appends a single-quoted Go character literal representing the rune, as generated by QuoteRune, to dst and returns the extended buffer.
func AppendQuoteRuneToASCII This function is used to appends a single-quoted Go character literal representing the rune, as generated by QuoteRuneToASCII, to dst and returns the extended buffer.
func AppendQuoteRuneToGraphic This function is used to appends a single-quoted Go character literal representing the rune, as generated by QuoteRuneToGraphic, to dst and returns the extended buffer.
func AppendQuoteToASCII This function is used to appends a double-quoted Go string literal representing s, as generated by QuoteToASCII, to dst and returns the extended buffer.
func AppendQuoteToGraphic This function is used to appends a double-quoted Go string literal representing s, as generated by QuoteToGraphic, to dst and returns the extended buffer.
func AppendUint This function is used to appends the string form of the unsigned integer i, as generated by FormatUint, to dst and returns the extended buffer.
func Atoi This function is equivalent to ParseInt(s, 10, 0) and converted to type int.
func CanBackquote This function is used to check whether the given string can be represented unchanged as a single-line backquoted string without control characters other than tab.
func FormatBool This function is used to return true or false according to the value of x.
func FormatFloat This function is used to converts the floating-point number f to a string, according to the format fmt and precision prec.
func FormatInt This function is used to return the string representation of i in the given base, for 2 <= base <= 36.
func FormatUint This function is used to return the string representation of i in the given base, for 2 <= base <= 36.
func IsGraphic This function is used to check whether the rune is defined as a Graphic by Unicode.
func IsPrint This function is used to check whether the rune is defined as printable by Go, with the same definition as unicode.IsPrint: letters, numbers, punctuation, symbols and ASCII space.
func Itoa This function is equivalent to FormatInt(int64(i), 10).
func ParseBool This function is used to returns the boolean value represented by the string.
func ParseFloat This function is used to converts the string s to a floating-point number with the precision specified by bitSize: 32 for float32, or 64 for float64.
func ParseInt This function interpret a string str in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i.
func ParseUint This function is like ParseInt but for unsigned numbers.
func Quote This function is used to returns a double-quoted Go string literal representing s.
func QuoteRune This function is used to returns a single-quoted Go character literal representing the rune.
func QuoteRuneToASCII This function is used to returns a single-quoted Go character literal representing the rune.
func QuoteRuneToGraphic This function is used to returns a single-quoted Go character literal representing the rune.
func QuoteToASCII This function is used to returns a double-quoted Go string literal representing s.
func QuoteToGraphic This function is used to returns a double-quoted Go string literal representing s.
func Unquote This function interpret s as a single-quoted, double-quoted, or backquoted Go string literal, returning the string value that s quotes.
func UnquoteChar This function decodes the first character or byte in the escaped string or character literal represented by the string s.

示例 1:

// Golang program to illustrate the 
// strconv.AppendQuoteRuneToASCII() function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
         // Converting Unicode characters to 
         // ASCII strings resulting from "single quotes"
         // append the result to the end of the given []byte
         // Using AppendQuoteRuneToASCII() function
         num := []byte("Rune : ")
         num = strconv.AppendQuoteRuneToASCII(num, 'c')
         fmt.Println(string(num))
  
}

输出:

Rune : 'c'

示例 2:

// Golang program to illustrate 
// strconv.Atoi() function 
package main 
   
import ( 
    "fmt"
    "strconv"
) 
   
func main() { 
   
    // Using Atoi() function 
    a := "244"
    b, e := strconv.Atoi(a) 
    if e == nil { 
        fmt.Printf("%T \n %v", b, b) 
    } 
   
} 

输出:

int 
 244