github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/internal/conv/conv.go (about) 1 package conv 2 3 import ( 4 "fmt" 5 "reflect" 6 ) 7 8 // InterfaceSlice converts an array or slice of any type into an []interface{} 9 // for use in functions that expect this. 10 func InterfaceSlice(slice interface{}) ([]interface{}, error) { 11 // avoid all this nonsense if this is already a []interface{}... 12 if s, ok := slice.([]interface{}); ok { 13 return s, nil 14 } 15 s := reflect.ValueOf(slice) 16 kind := s.Kind() 17 switch kind { 18 case reflect.Slice, reflect.Array: 19 l := s.Len() 20 ret := make([]interface{}, l) 21 for i := 0; i < l; i++ { 22 ret[i] = s.Index(i).Interface() 23 } 24 return ret, nil 25 default: 26 return nil, fmt.Errorf("expected an array or slice, but got a %T", s) 27 } 28 }