util.go 836 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package service
  2. // InSliceIface checks given interface in interface slice.
  3. func InSliceIface(v interface{}, sl []interface{}) bool {
  4. for _, vv := range sl {
  5. if vv == v {
  6. return true
  7. }
  8. }
  9. return false
  10. }
  11. // SliceUnique cleans repeated values in slice.
  12. func SliceUnique(slice []interface{}) (uniqueslice []interface{}) {
  13. for _, v := range slice {
  14. if !InSliceIface(v, uniqueslice) {
  15. uniqueslice = append(uniqueslice, v)
  16. }
  17. }
  18. return
  19. }
  20. // Slice2String convert slice to string
  21. func Slice2String(slice []interface{}) (uniqueslice []string) {
  22. for _, v := range slice {
  23. uniqueslice = append(uniqueslice, v.(string))
  24. }
  25. return
  26. }
  27. // Slice2Interface convert slice to interface
  28. func Slice2Interface(slice []string) (uniqueslice []interface{}) {
  29. for _, v := range slice {
  30. uniqueslice = append(uniqueslice, v)
  31. }
  32. return
  33. }