utils_is.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package utils
  7. import (
  8. "github.com/gogf/gf/internal/empty"
  9. "reflect"
  10. )
  11. // IsNil checks whether `value` is nil.
  12. func IsNil(value interface{}) bool {
  13. return value == nil
  14. }
  15. // IsEmpty checks whether `value` is empty.
  16. func IsEmpty(value interface{}) bool {
  17. return empty.IsEmpty(value)
  18. }
  19. // IsInt checks whether `value` is type of int.
  20. func IsInt(value interface{}) bool {
  21. switch value.(type) {
  22. case int, *int, int8, *int8, int16, *int16, int32, *int32, int64, *int64:
  23. return true
  24. }
  25. return false
  26. }
  27. // IsUint checks whether `value` is type of uint.
  28. func IsUint(value interface{}) bool {
  29. switch value.(type) {
  30. case uint, *uint, uint8, *uint8, uint16, *uint16, uint32, *uint32, uint64, *uint64:
  31. return true
  32. }
  33. return false
  34. }
  35. // IsFloat checks whether `value` is type of float.
  36. func IsFloat(value interface{}) bool {
  37. switch value.(type) {
  38. case float32, *float32, float64, *float64:
  39. return true
  40. }
  41. return false
  42. }
  43. // IsSlice checks whether `value` is type of slice.
  44. func IsSlice(value interface{}) bool {
  45. var (
  46. reflectValue = reflect.ValueOf(value)
  47. reflectKind = reflectValue.Kind()
  48. )
  49. for reflectKind == reflect.Ptr {
  50. reflectValue = reflectValue.Elem()
  51. }
  52. switch reflectKind {
  53. case reflect.Slice, reflect.Array:
  54. return true
  55. }
  56. return false
  57. }
  58. // IsMap checks whether `value` is type of map.
  59. func IsMap(value interface{}) bool {
  60. var (
  61. reflectValue = reflect.ValueOf(value)
  62. reflectKind = reflectValue.Kind()
  63. )
  64. for reflectKind == reflect.Ptr {
  65. reflectValue = reflectValue.Elem()
  66. }
  67. switch reflectKind {
  68. case reflect.Map:
  69. return true
  70. }
  71. return false
  72. }
  73. // IsStruct checks whether `value` is type of struct.
  74. func IsStruct(value interface{}) bool {
  75. var (
  76. reflectValue = reflect.ValueOf(value)
  77. reflectKind = reflectValue.Kind()
  78. )
  79. for reflectKind == reflect.Ptr {
  80. reflectValue = reflectValue.Elem()
  81. reflectKind = reflectValue.Kind()
  82. }
  83. switch reflectKind {
  84. case reflect.Struct:
  85. return true
  86. }
  87. return false
  88. }