Go Lang - 接口和数组

use*_*584 -1 arrays interface go

我有一个我认为类型为"[] interface {}"的变量

  1. 我如何检测它
  2. 转换为数组?

这是代码:

var s string
switch value1 := value1.(type) {
case int:
    s = strconv.Itoa(value1)
case float64:
    s = strconv.FormatFloat(value1, 'f', 0, 64)
//case array:
    //fmt.Printf("array")
default :
    fmt.Printf("\nvalue=v+%",value1)
}
Run Code Online (Sandbox Code Playgroud)

输出是:

value=v+%!(NOVERB)%!(EXTRA []interface {}=
Run Code Online (Sandbox Code Playgroud)

Jam*_*dge 6

您可以在类型开关中选择与其他类型相同的切片.例如:

switch v := value1.(type) {
case []interface{}:
    for _, element := range v {
        fmt.Println(element)
    }
} 
Run Code Online (Sandbox Code Playgroud)

你可以在这里玩这个例子:http://play.golang.org/p/4z9eejp4BL