utils_is.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "reflect"
  9. "github.com/gogf/gf/v2/internal/empty"
  10. )
  11. // IsNil checks whether `value` is nil, especially for interface{} type value.
  12. func IsNil(value interface{}) bool {
  13. return empty.IsNil(value)
  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. reflectKind = reflectValue.Kind()
  52. }
  53. switch reflectKind {
  54. case reflect.Slice, reflect.Array:
  55. return true
  56. }
  57. return false
  58. }
  59. // IsMap checks whether `value` is type of map.
  60. func IsMap(value interface{}) bool {
  61. var (
  62. reflectValue = reflect.ValueOf(value)
  63. reflectKind = reflectValue.Kind()
  64. )
  65. for reflectKind == reflect.Ptr {
  66. reflectValue = reflectValue.Elem()
  67. reflectKind = reflectValue.Kind()
  68. }
  69. switch reflectKind {
  70. case reflect.Map:
  71. return true
  72. }
  73. return false
  74. }
  75. // IsStruct checks whether `value` is type of struct.
  76. func IsStruct(value interface{}) bool {
  77. var reflectType = reflect.TypeOf(value)
  78. if reflectType == nil {
  79. return false
  80. }
  81. var reflectKind = reflectType.Kind()
  82. for reflectKind == reflect.Ptr {
  83. reflectType = reflectType.Elem()
  84. reflectKind = reflectType.Kind()
  85. }
  86. switch reflectKind {
  87. case reflect.Struct:
  88. return true
  89. }
  90. return false
  91. }