ToString returns val as a string, as efficiently as possible. EXPERIMENTAL: may be changed or removed later.
(val any)
| 289 | // ToString returns val as a string, as efficiently as possible. |
| 290 | // EXPERIMENTAL: may be changed or removed later. |
| 291 | func ToString(val any) string { |
| 292 | switch v := val.(type) { |
| 293 | case nil: |
| 294 | return "" |
| 295 | case string: |
| 296 | return v |
| 297 | case fmt.Stringer: |
| 298 | return v.String() |
| 299 | case error: |
| 300 | return v.Error() |
| 301 | case byte: |
| 302 | return string(v) |
| 303 | case []byte: |
| 304 | return string(v) |
| 305 | case []rune: |
| 306 | return string(v) |
| 307 | case int: |
| 308 | return strconv.Itoa(v) |
| 309 | case int32: |
| 310 | return strconv.Itoa(int(v)) |
| 311 | case int64: |
| 312 | return strconv.Itoa(int(v)) |
| 313 | case uint: |
| 314 | return strconv.FormatUint(uint64(v), 10) |
| 315 | case uint32: |
| 316 | return strconv.FormatUint(uint64(v), 10) |
| 317 | case uint64: |
| 318 | return strconv.FormatUint(v, 10) |
| 319 | case float32: |
| 320 | return strconv.FormatFloat(float64(v), 'f', -1, 32) |
| 321 | case float64: |
| 322 | return strconv.FormatFloat(v, 'f', -1, 64) |
| 323 | case bool: |
| 324 | if v { |
| 325 | return "true" |
| 326 | } |
| 327 | return "false" |
| 328 | default: |
| 329 | return fmt.Sprintf("%+v", v) |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // ReplacerFunc is a function that returns a replacement for the |
| 334 | // given key along with true if the function is able to service |