ToStringE casts any value to a string type.
(i any)
| 68 | |
| 69 | // ToStringE casts any value to a string type. |
| 70 | func ToStringE(i any) (string, error) { |
| 71 | switch s := i.(type) { |
| 72 | case string: |
| 73 | return s, nil |
| 74 | case bool: |
| 75 | return strconv.FormatBool(s), nil |
| 76 | case float64: |
| 77 | return strconv.FormatFloat(s, 'f', -1, 64), nil |
| 78 | case float32: |
| 79 | return strconv.FormatFloat(float64(s), 'f', -1, 32), nil |
| 80 | case int: |
| 81 | return strconv.Itoa(s), nil |
| 82 | case int8: |
| 83 | return strconv.FormatInt(int64(s), 10), nil |
| 84 | case int16: |
| 85 | return strconv.FormatInt(int64(s), 10), nil |
| 86 | case int32: |
| 87 | return strconv.FormatInt(int64(s), 10), nil |
| 88 | case int64: |
| 89 | return strconv.FormatInt(s, 10), nil |
| 90 | case uint: |
| 91 | return strconv.FormatUint(uint64(s), 10), nil |
| 92 | case uint8: |
| 93 | return strconv.FormatUint(uint64(s), 10), nil |
| 94 | case uint16: |
| 95 | return strconv.FormatUint(uint64(s), 10), nil |
| 96 | case uint32: |
| 97 | return strconv.FormatUint(uint64(s), 10), nil |
| 98 | case uint64: |
| 99 | return strconv.FormatUint(s, 10), nil |
| 100 | case json.Number: |
| 101 | return s.String(), nil |
| 102 | case []byte: |
| 103 | return string(s), nil |
| 104 | case template.HTML: |
| 105 | return string(s), nil |
| 106 | case template.URL: |
| 107 | return string(s), nil |
| 108 | case template.JS: |
| 109 | return string(s), nil |
| 110 | case template.CSS: |
| 111 | return string(s), nil |
| 112 | case template.HTMLAttr: |
| 113 | return string(s), nil |
| 114 | case nil: |
| 115 | return "", nil |
| 116 | case fmt.Stringer: |
| 117 | return s.String(), nil |
| 118 | case error: |
| 119 | return s.Error(), nil |
| 120 | default: |
| 121 | if i, ok := indirect(i); ok { |
| 122 | return ToStringE(i) |
| 123 | } |
| 124 | |
| 125 | if i, ok := resolveAlias(i); ok { |
| 126 | return ToStringE(i) |
| 127 | } |
no test coverage detected
searching dependent graphs…