SafeStringValue safely converts various types to string representation. This function provides a safe way to convert interface{} values to strings, handling multiple common types that can appear in JSON or API responses. It never panics and always returns a string value. Supported conversions: - s
(value interface{})
| 297 | // str := SafeStringValue(true) // "true" |
| 298 | // str := SafeStringValue(nil) // "" |
| 299 | func SafeStringValue(value interface{}) string { |
| 300 | if value == nil { |
| 301 | return "" |
| 302 | } |
| 303 | |
| 304 | switch v := value.(type) { |
| 305 | case string: |
| 306 | return v |
| 307 | case int: |
| 308 | return strconv.Itoa(v) |
| 309 | case float64: |
| 310 | return strconv.FormatFloat(v, 'f', -1, 64) |
| 311 | case bool: |
| 312 | return strconv.FormatBool(v) |
| 313 | default: |
| 314 | return fmt.Sprintf("%v", v) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // SafeFloatValue safely converts various types to float64 representation. |
| 319 | // |
no outgoing calls