| Total Lines | 27 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | package internal |
||
| 2 | |||
| 3 | import ( |
||
| 4 | "reflect" |
||
| 5 | |||
| 6 | "github.com/gotilty/gotil/internal/errs" |
||
| 7 | ) |
||
| 8 | |||
| 9 | // Size returns a length of given parameter |
||
| 10 | // data := []int64{100, 30, -100, -5} |
||
| 11 | // result, _ := Size(data) |
||
| 12 | // result2, _ := Size("gotil") |
||
| 13 | // // Output: 4 |
||
| 14 | // // Output2: 5 |
||
| 15 | func Size(val interface{}) (int, error) { |
||
| 16 | value := reflect.ValueOf(val) |
||
| 17 | switch value.Kind() { |
||
| 18 | case reflect.Map, reflect.Struct, reflect.Array, reflect.Slice: |
||
| 19 | return value.Len(), nil |
||
| 20 | case reflect.String: |
||
| 21 | val_str, err := ToString(val) |
||
| 22 | if err == nil { |
||
| 23 | return len(val_str), nil |
||
| 24 | } |
||
| 25 | return 0, errs.CustomError(value.Kind().String()) |
||
| 26 | default: |
||
| 27 | return 0, errs.CustomError(value.Kind().String()) |
||
| 28 | } |
||
| 30 |