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