utils_is.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. }
  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 reflectType = reflect.TypeOf(value)
  76. if reflectType == nil {
  77. return false
  78. }
  79. var reflectKind = reflectType.Kind()
  80. for reflectKind == reflect.Ptr {
  81. reflectType = reflectType.Elem()
  82. reflectKind = reflectType.Kind()
  83. }
  84. switch reflectKind {
  85. case reflect.Struct:
  86. return true
  87. }
  88. return false
  89. }