Go Lang - 接口和数组

时间:2014-01-31 07:57:51

标签: arrays interface go

我有一个我认为类型为“[] interface {}”

的变量
  1. 我如何检测
  2. 转换为数组?
  3. 以下是代码:

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

    输出是:

    value=v+%!(NOVERB)%!(EXTRA []interface {}=
    

1 个答案:

答案 0 :(得分:6)

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

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

您可以在此处使用此示例:http://play.golang.org/p/4z9eejp4BL