cast.go (4005B)
1 // Copyright © 2014 Steve Francia <spf@spf13.com>. 2 // 3 // Use of this source code is governed by an MIT-style 4 // license that can be found in the LICENSE file. 5 6 // Package cast provides easy and safe casting in Go. 7 package cast 8 9 import "time" 10 11 // ToBool casts an interface to a bool type. 12 func ToBool(i interface{}) bool { 13 v, _ := ToBoolE(i) 14 return v 15 } 16 17 // ToTime casts an interface to a time.Time type. 18 func ToTime(i interface{}) time.Time { 19 v, _ := ToTimeE(i) 20 return v 21 } 22 23 func ToTimeInDefaultLocation(i interface{}, location *time.Location) time.Time { 24 v, _ := ToTimeInDefaultLocationE(i, location) 25 return v 26 } 27 28 // ToDuration casts an interface to a time.Duration type. 29 func ToDuration(i interface{}) time.Duration { 30 v, _ := ToDurationE(i) 31 return v 32 } 33 34 // ToFloat64 casts an interface to a float64 type. 35 func ToFloat64(i interface{}) float64 { 36 v, _ := ToFloat64E(i) 37 return v 38 } 39 40 // ToFloat32 casts an interface to a float32 type. 41 func ToFloat32(i interface{}) float32 { 42 v, _ := ToFloat32E(i) 43 return v 44 } 45 46 // ToInt64 casts an interface to an int64 type. 47 func ToInt64(i interface{}) int64 { 48 v, _ := ToInt64E(i) 49 return v 50 } 51 52 // ToInt32 casts an interface to an int32 type. 53 func ToInt32(i interface{}) int32 { 54 v, _ := ToInt32E(i) 55 return v 56 } 57 58 // ToInt16 casts an interface to an int16 type. 59 func ToInt16(i interface{}) int16 { 60 v, _ := ToInt16E(i) 61 return v 62 } 63 64 // ToInt8 casts an interface to an int8 type. 65 func ToInt8(i interface{}) int8 { 66 v, _ := ToInt8E(i) 67 return v 68 } 69 70 // ToInt casts an interface to an int type. 71 func ToInt(i interface{}) int { 72 v, _ := ToIntE(i) 73 return v 74 } 75 76 // ToUint casts an interface to a uint type. 77 func ToUint(i interface{}) uint { 78 v, _ := ToUintE(i) 79 return v 80 } 81 82 // ToUint64 casts an interface to a uint64 type. 83 func ToUint64(i interface{}) uint64 { 84 v, _ := ToUint64E(i) 85 return v 86 } 87 88 // ToUint32 casts an interface to a uint32 type. 89 func ToUint32(i interface{}) uint32 { 90 v, _ := ToUint32E(i) 91 return v 92 } 93 94 // ToUint16 casts an interface to a uint16 type. 95 func ToUint16(i interface{}) uint16 { 96 v, _ := ToUint16E(i) 97 return v 98 } 99 100 // ToUint8 casts an interface to a uint8 type. 101 func ToUint8(i interface{}) uint8 { 102 v, _ := ToUint8E(i) 103 return v 104 } 105 106 // ToString casts an interface to a string type. 107 func ToString(i interface{}) string { 108 v, _ := ToStringE(i) 109 return v 110 } 111 112 // ToStringMapString casts an interface to a map[string]string type. 113 func ToStringMapString(i interface{}) map[string]string { 114 v, _ := ToStringMapStringE(i) 115 return v 116 } 117 118 // ToStringMapStringSlice casts an interface to a map[string][]string type. 119 func ToStringMapStringSlice(i interface{}) map[string][]string { 120 v, _ := ToStringMapStringSliceE(i) 121 return v 122 } 123 124 // ToStringMapBool casts an interface to a map[string]bool type. 125 func ToStringMapBool(i interface{}) map[string]bool { 126 v, _ := ToStringMapBoolE(i) 127 return v 128 } 129 130 // ToStringMapInt casts an interface to a map[string]int type. 131 func ToStringMapInt(i interface{}) map[string]int { 132 v, _ := ToStringMapIntE(i) 133 return v 134 } 135 136 // ToStringMapInt64 casts an interface to a map[string]int64 type. 137 func ToStringMapInt64(i interface{}) map[string]int64 { 138 v, _ := ToStringMapInt64E(i) 139 return v 140 } 141 142 // ToStringMap casts an interface to a map[string]interface{} type. 143 func ToStringMap(i interface{}) map[string]interface{} { 144 v, _ := ToStringMapE(i) 145 return v 146 } 147 148 // ToSlice casts an interface to a []interface{} type. 149 func ToSlice(i interface{}) []interface{} { 150 v, _ := ToSliceE(i) 151 return v 152 } 153 154 // ToBoolSlice casts an interface to a []bool type. 155 func ToBoolSlice(i interface{}) []bool { 156 v, _ := ToBoolSliceE(i) 157 return v 158 } 159 160 // ToStringSlice casts an interface to a []string type. 161 func ToStringSlice(i interface{}) []string { 162 v, _ := ToStringSliceE(i) 163 return v 164 } 165 166 // ToIntSlice casts an interface to a []int type. 167 func ToIntSlice(i interface{}) []int { 168 v, _ := ToIntSliceE(i) 169 return v 170 } 171 172 // ToDurationSlice casts an interface to a []time.Duration type. 173 func ToDurationSlice(i interface{}) []time.Duration { 174 v, _ := ToDurationSliceE(i) 175 return v 176 }