-
Notifications
You must be signed in to change notification settings - Fork 112
/
type.go
33 lines (29 loc) · 822 Bytes
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package util
import (
"fmt"
"reflect"
)
// AsString returns the argument in a 'string' form no matter what type it is.
func AsString(x interface{}) string {
if reflect.TypeOf(x).Kind() == reflect.String {
return x.(string)
}
return fmt.Sprint(x)
}
// IsNumber returns true if the argument is a numeric type; false otherwise.
func IsNumber(x interface{}) bool {
switch reflect.TypeOf(x).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return true
}
return false
}
// IsString returns true if the argument is a string; false otherwise.
func IsString(x interface{}) bool {
if reflect.TypeOf(x).Kind() == reflect.String {
return true
}
return false
}