1. package main
  2. import (
  3. "fmt"
  4. )
  5. func printAll(values []interface{}) {
  6. for _, val := range values {
  7. fmt.Println(val)
  8. }
  9. }
  10. func main() {
  11. name := []string{"littleGirl", "playBoy", "1"}
  12. values := make([]interface{}, len(name))
  13. for k, v := range name {
  14. values[k] = v
  15. }
  16. printAll(values)
  17. }

然而,需要注意的是,[]T不能直接赋值给[]interface{}
这样子会有错误的.

  1. t := []int{1, 2, 3, 4}
  2. var s []interface{} = t

编译时会输出下面的错误

cannot use t (type []int) as type []interface {} in assignment

分类: web

标签:   golang