GetInt retrieves an int value from the options. If the key does not exist, GetInt returns the provided default value. If the key exists but the value is of a different integer type, GetInt converts it to int. If the key exists but the value is not an integer type, GetInt panics.
(key string, def int)
| 189 | // GetInt converts it to int. |
| 190 | // If the key exists but the value is not an integer type, GetInt panics. |
| 191 | func (o *Options) GetInt(key string, def int) int { |
| 192 | v, ok := o.m[key] |
| 193 | if !ok { |
| 194 | return def |
| 195 | } |
| 196 | |
| 197 | switch t := v.(type) { |
| 198 | case int: |
| 199 | return t |
| 200 | case int8: |
| 201 | return int(t) |
| 202 | case int16: |
| 203 | return int(t) |
| 204 | case int32: |
| 205 | return int(t) |
| 206 | case int64: |
| 207 | if t > int64(math.MaxInt) || t < int64(math.MinInt) { |
| 208 | panic(fmt.Errorf("value %d for key %q exceeds int range", t, key)) |
| 209 | } |
| 210 | return int(t) |
| 211 | case uint: |
| 212 | if t > uint(math.MaxInt) { |
| 213 | panic(fmt.Errorf("value %d for key %q exceeds int range", t, key)) |
| 214 | } |
| 215 | return int(t) |
| 216 | case uint8: |
| 217 | return int(t) |
| 218 | case uint16: |
| 219 | return int(t) |
| 220 | case uint32: |
| 221 | return int(t) |
| 222 | case uint64: |
| 223 | if t > uint64(math.MaxInt) { |
| 224 | panic(fmt.Errorf("value %d for key %q exceeds int range", t, key)) |
| 225 | } |
| 226 | return int(t) |
| 227 | default: |
| 228 | panic(newTypeMismatchError(key, v, def)) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // GetFloat retrieves a float64 value from the options. |
| 233 | // If the key does not exist, GetFloat returns the provided default value. |