ValToString converts a simple value to a string. It does not handle complex values (arrays and mappings).
(v interface{})
| 587 | // ValToString converts a simple value to a string. |
| 588 | // It does not handle complex values (arrays and mappings). |
| 589 | func ValToString(v interface{}) string { |
| 590 | switch v := v.(type) { |
| 591 | case float64: |
| 592 | result := strconv.FormatFloat(v, 'G', -1, 64) |
| 593 | // If the result can be confused with an integer, make sure we have at least one decimal digit |
| 594 | if !strings.ContainsRune(result, '.') && !strings.ContainsRune(result, 'E') { |
| 595 | result = strconv.FormatFloat(v, 'f', 1, 64) |
| 596 | } |
| 597 | return result |
| 598 | case bool: |
| 599 | return strconv.FormatBool(v) |
| 600 | case time.Time: |
| 601 | return v.Format(time.RFC3339) |
| 602 | case fmt.Stringer: |
| 603 | return v.String() |
| 604 | default: |
| 605 | return fmt.Sprintf("%v", v) |
| 606 | } |
| 607 | } |