format is the main workhorse for providing the Formatter interface. It uses the passed reflect value to figure out what kind of object we are dealing with and formats it appropriately. It is a recursive function, however circular data structures are detected and handled properly.
(v reflect.Value)
| 199 | // dealing with and formats it appropriately. It is a recursive function, |
| 200 | // however circular data structures are detected and handled properly. |
| 201 | func (f *formatState) format(v reflect.Value) { |
| 202 | // Handle invalid reflect values immediately. |
| 203 | kind := v.Kind() |
| 204 | if kind == reflect.Invalid { |
| 205 | f.fs.Write(invalidAngleBytes) |
| 206 | return |
| 207 | } |
| 208 | |
| 209 | // Handle pointers specially. |
| 210 | if kind == reflect.Ptr { |
| 211 | f.formatPtr(v) |
| 212 | return |
| 213 | } |
| 214 | |
| 215 | // Print type information unless already handled elsewhere. |
| 216 | if !f.ignoreNextType && f.fs.Flag('#') { |
| 217 | f.fs.Write(openParenBytes) |
| 218 | f.fs.Write([]byte(v.Type().String())) |
| 219 | f.fs.Write(closeParenBytes) |
| 220 | } |
| 221 | f.ignoreNextType = false |
| 222 | |
| 223 | // Call Stringer/error interfaces if they exist and the handle methods |
| 224 | // flag is enabled. |
| 225 | if !f.cs.DisableMethods { |
| 226 | if (kind != reflect.Invalid) && (kind != reflect.Interface) { |
| 227 | if handled := handleMethods(f.cs, f.fs, v); handled { |
| 228 | return |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | switch kind { |
| 234 | case reflect.Invalid: |
| 235 | // Do nothing. We should never get here since invalid has already |
| 236 | // been handled above. |
| 237 | |
| 238 | case reflect.Bool: |
| 239 | printBool(f.fs, v.Bool()) |
| 240 | |
| 241 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: |
| 242 | printInt(f.fs, v.Int(), 10) |
| 243 | |
| 244 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: |
| 245 | printUint(f.fs, v.Uint(), 10) |
| 246 | |
| 247 | case reflect.Float32: |
| 248 | printFloat(f.fs, v.Float(), 32) |
| 249 | |
| 250 | case reflect.Float64: |
| 251 | printFloat(f.fs, v.Float(), 64) |
| 252 | |
| 253 | case reflect.Complex64: |
| 254 | printComplex(f.fs, v.Complex(), 32) |
| 255 | |
| 256 | case reflect.Complex128: |
| 257 | printComplex(f.fs, v.Complex(), 64) |
| 258 |