go类型转换断言与转换(及strconv包)

go语言是强类型的语言,所以类型转换是必不可少的。Go语言要求表达式的不同的类型之间必须做显示的类型转换;不过Go语言必须做显示的类型转换的要求也有例外的情况:

  • 当普通T类型变量向I接口类型转换时是隐式的;

  • 当IX接口变量向I接口类型转换可以在编译期完成时是隐式的;


类型转换表达式T(x)

T(x) 用于把变量x转换为T类型;非常量值x能转换为T需要满足:

  • x能赋值给T类型;

  • x的类型与T有相同的底层类型(underlying type);

  • x的类型与T都是未命名的指针类型,切指针基于的类型是相容的;

  • x的类型与T都是整数类型,或浮点类型;

  • x的类型与T都是复数类型;

  • x是数字或byte/rune的切片,T为string;

  • x是string类型,T为byte/rune的切片;

 x需要转换为T类型的语法是T(x).

 *Point(p)        // same as *(Point(p))

(*Point)(p)      // p is converted to *Point

<-chan int(c)    // same as <-(chan int(c))

(<-chan int)(c)  // c is converted to <-chan int

func()(x)        // function signature func() x

(func())(x)      // x is converted to func()

(func() int)(x)  // x is converted to func() int

func() int(x)    // x is converted to func() int (unambiguous)

还有一个容易混淆的地方是 只读通道(<-chan int)和只写通道(chan<- int)类型。


接口interface{}类型转换

interface{}可用于向函数传递任意类型的变量,在函数内部使用时需要通过类型断言来提取指定的明确类型的值。

v, ok := value.(typeName)

转换成功,结果v拥有静态类型 typeName 的新值;失败,ok返回false。

v= value.(typeName)转换失败是会抛出恐慌。

func VarType(info interface{}){

// if v, ok := info.(int); ok{

//  fmt.Println("int: ", v)

//  result = v

// }else if v, ok := info.(string); ok{

//  fmt.Println("string: ", v)

// }else {

//  fmt.Printf("%v\n", info)

// }

        // 直接通过switch-type判断

switch v := info.(type){

case int: fmt.Println("int: ", v)

case string: fmt.Println("string: ", v)

default: fmt.Printf("Deufalt: %v\n", info)

}

}


string与基本类型间转换strconv

strconv包中实现基本类型与string间的互相转换。包括四类函数:

  • Append 类:如 AppendBool(dst []byte, b bool)[]byte,将值转化后添加到[]byte的末尾

  • Format 类,例如FormatBool(b bool) string,将bool  float int uint 类型的转换为string,FormatInt的缩写为Itoa

  • Parse 类,例如ParseBool(str string)(value bool, err error) 将字符串转换为 bool float int uint类型的值,err指定是否转换成功,ParseInt的缩写是Atoi

  • Quote 类,对字符串的 双引号 单引号 反单引号 的操作

数字与字符串间转换

i, err := strconv.Atoi("-42")

s := strconv.Itoa(-42)

b, err := strconv.ParseBool("true")

f, err := strconv.ParseFloat("3.1415", 64)

i, err := strconv.ParseInt("-42", 10, 64)

u, err := strconv.ParseUint("42", 10, 64)

s := strconv.FormatBool(true)

s := strconv.FormatFloat(3.1415, 'E', -1, 64)

s := strconv.FormatInt(-42, 16)

s := strconv.FormatUint(42, 16)

字符串转换quote:将字符串转换为双引号引起的字符串(特殊字符串替换为转移字符):

strconv.Quote(`C:\Windows`) // "C:\\Windows"

strconv.QuoteToASCII("Hello 世界!") // "Hello \u4e16\u754c\uff01"

转换函数

  • 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 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 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)

猜你喜欢

转载自blog.csdn.net/alwaysrun/article/details/82919143