| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
 | <h1> Package fmt  </h1>     <ul id="short-nav">
<li><code>import "fmt"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler. </p>
<h3 id="hdr-Printing">Printing</h3> <p>The verbs: </p>
<p>General: </p>
<pre data-language="go">%v	the value in a default format
	when printing structs, the plus flag (%+v) adds field names
%#v	a Go-syntax representation of the value
%T	a Go-syntax representation of the type of the value
%%	a literal percent sign; consumes no value
</pre> <p>Boolean: </p>
<pre data-language="go">%t	the word true or false
</pre> <p>Integer: </p>
<pre data-language="go">%b	base 2
%c	the character represented by the corresponding Unicode code point
%d	base 10
%o	base 8
%O	base 8 with 0o prefix
%q	a single-quoted character literal safely escaped with Go syntax.
%x	base 16, with lower-case letters for a-f
%X	base 16, with upper-case letters for A-F
%U	Unicode format: U+1234; same as "U+%04X"
</pre> <p>Floating-point and complex constituents: </p>
<pre data-language="go">%b	decimalless scientific notation with exponent a power of two,
	in the manner of strconv.FormatFloat with the 'b' format,
	e.g. -123456p-78
%e	scientific notation, e.g. -1.234456e+78
%E	scientific notation, e.g. -1.234456E+78
%f	decimal point but no exponent, e.g. 123.456
%F	synonym for %f
%g	%e for large exponents, %f otherwise. Precision is discussed below.
%G	%E for large exponents, %F otherwise
%x	hexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20
%X	upper-case hexadecimal notation, e.g. -0X1.23ABCP+20
</pre> <p>String and slice of bytes (treated equivalently with these verbs): </p>
<pre data-language="go">%s	the uninterpreted bytes of the string or slice
%q	a double-quoted string safely escaped with Go syntax
%x	base 16, lower-case, two characters per byte
%X	base 16, upper-case, two characters per byte
</pre> <p>Slice: </p>
<pre data-language="go">%p	address of 0th element in base 16 notation, with leading 0x
</pre> <p>Pointer: </p>
<pre data-language="go">%p	base 16 notation, with leading 0x
The %b, %d, %o, %x and %X verbs also work with pointers,
formatting the value exactly as if it were an integer.
</pre> <p>The default format for %v is: </p>
<pre data-language="go">bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %#x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p
</pre> <p>For compound objects, the elements are printed using these rules, recursively, laid out like this: </p>
<pre data-language="go">struct:             {field0 field1 ...}
array, slice:       [elem0 elem1 ...]
maps:               map[key1:value1 key2:value2 ...]
pointer to above:   &{}, &[], &map[]
</pre> <p>Width is specified by an optional decimal number immediately preceding the verb. If absent, the width is whatever is necessary to represent the value. Precision is specified after the (optional) width by a period followed by a decimal number. If no period is present, a default precision is used. A period with no following number specifies a precision of zero. Examples: </p>
<pre data-language="go">%f     default width, default precision
%9f    width 9, default precision
%.2f   default width, precision 2
%9.2f  width 9, precision 2
%9.f   width 9, precision 0
</pre> <p>Width and precision are measured in units of Unicode code points, that is, runes. (This differs from C's printf where the units are always measured in bytes.) Either or both of the flags may be replaced with the character '*', causing their values to be obtained from the next operand (preceding the one to format), which must be of type int. </p>
<p>For most values, width is the minimum number of runes to output, padding the formatted form with spaces if necessary. </p>
<p>For strings, byte slices and byte arrays, however, precision limits the length of the input to be formatted (not the size of the output), truncating if necessary. Normally it is measured in runes, but for these types when formatted with the %x or %X format it is measured in bytes. </p>
<p>For floating-point values, width sets the minimum width of the field and precision sets the number of places after the decimal, if appropriate, except that for %g/%G precision sets the maximum number of significant digits (trailing zeros are removed). For example, given 12.345 the format %6.3f prints 12.345 while %.3g prints 12.3. The default precision for %e, %f and %#g is 6; for %g it is the smallest number of digits necessary to identify the value uniquely. </p>
<p>For complex numbers, the width and precision apply to the two components independently and the result is parenthesized, so %f applied to 1.2+3.4i produces (1.200000+3.400000i). </p>
<p>When formatting a single integer code point or a rune string (type []rune) with %q, invalid Unicode code points are changed to the Unicode replacement character, U+FFFD, as in strconv.QuoteRune. </p>
<p>Other flags: </p>
<pre data-language="go">'+'	always print a sign for numeric values;
	guarantee ASCII-only output for %q (%+q)
'-'	pad with spaces on the right rather than the left (left-justify the field)
'#'	alternate format: add leading 0b for binary (%#b), 0 for octal (%#o),
	0x or 0X for hex (%#x or %#X); suppress 0x for %p (%#p);
	for %q, print a raw (backquoted) string if strconv.CanBackquote
	returns true;
	always print a decimal point for %e, %E, %f, %F, %g and %G;
	do not remove trailing zeros for %g and %G;
	write e.g. U+0078 'x' if the character is printable for %U (%#U).
' '	(space) leave a space for elided sign in numbers (% d);
	put spaces between bytes printing strings or slices in hex (% x, % X)
'0'	pad with leading zeros rather than spaces;
	for numbers, this moves the padding after the sign;
	ignored for strings, byte slices and byte arrays
</pre> <p>Flags are ignored by verbs that do not expect them. For example there is no alternate decimal format, so %#d and %d behave identically. </p>
<p>For each Printf-like function, there is also a Print function that takes no format and is equivalent to saying %v for every operand. Another variant Println inserts blanks between operands and appends a newline. </p>
<p>Regardless of the verb, if an operand is an interface value, the internal concrete value is used, not the interface itself. Thus: </p>
<pre data-language="go">var i interface{} = 23
fmt.Printf("%v\n", i)
</pre> <p>will print 23. </p>
<p>Except when printed using the verbs %T and %p, special formatting considerations apply for operands that implement certain interfaces. In order of application: </p>
<p>1. If the operand is a reflect.Value, the operand is replaced by the concrete value that it holds, and printing continues with the next rule. </p>
<p>2. If an operand implements the Formatter interface, it will be invoked. In this case the interpretation of verbs and flags is controlled by that implementation. </p>
<p>3. If the %v verb is used with the # flag (%#v) and the operand implements the GoStringer interface, that will be invoked. </p>
<p>If the format (which is implicitly %v for Println etc.) is valid for a string (%s %q %x %X), or is %v but not %#v, the following two rules apply: </p>
<p>4. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any). </p>
<p>5. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any). </p>
<p>For compound operands such as slices and structs, the format applies to the elements of each operand, recursively, not to the operand as a whole. Thus %q will quote each element of a slice of strings, and %6.2f will control formatting for each element of a floating-point array. </p>
<p>However, when printing a byte slice with a string-like verb (%s %q %x %X), it is treated identically to a string, as a single item. </p>
<p>To avoid recursion in cases such as </p>
<pre data-language="go">type X string
func (x X) String() string { return Sprintf("<%s>", x) }
</pre> <p>convert the value before recurring: </p>
<pre data-language="go">func (x X) String() string { return Sprintf("<%s>", string(x)) }
</pre> <p>Infinite recursion can also be triggered by self-referential data structures, such as a slice that contains itself as an element, if that type has a String method. Such pathologies are rare, however, and the package does not protect against them. </p>
<p>When printing a struct, fmt cannot and therefore does not invoke formatting methods such as Error or String on unexported fields. </p>
<h3 id="hdr-Explicit_argument_indexes">Explicit argument indexes</h3> <p>In Printf, Sprintf, and Fprintf, the default behavior is for each formatting verb to format successive arguments passed in the call. However, the notation [n] immediately before the verb indicates that the nth one-indexed argument is to be formatted instead. The same notation before a '*' for a width or precision selects the argument index holding the value. After processing a bracketed expression [n], subsequent verbs will use arguments n+1, n+2, etc. unless otherwise directed. </p>
<p>For example, </p>
<pre data-language="go">fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
</pre> <p>will yield "22 11", while </p>
<pre data-language="go">fmt.Sprintf("%[3]*.[2]*[1]f", 12.0, 2, 6)
</pre> <p>equivalent to </p>
<pre data-language="go">fmt.Sprintf("%6.2f", 12.0)
</pre> <p>will yield " 12.00". Because an explicit index affects subsequent verbs, this notation can be used to print the same values multiple times by resetting the index for the first argument to be repeated: </p>
<pre data-language="go">fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
</pre> <p>will yield "16 17 0x10 0x11". </p>
<h3 id="hdr-Format_errors">Format errors</h3> <p>If an invalid argument is given for a verb, such as providing a string to %d, the generated string will contain a description of the problem, as in these examples: </p>
<pre data-language="go">Wrong type or unknown verb: %!verb(type=value)
	Printf("%d", "hi"):        %!d(string=hi)
Too many arguments: %!(EXTRA type=value)
	Printf("hi", "guys"):      hi%!(EXTRA string=guys)
Too few arguments: %!verb(MISSING)
	Printf("hi%d"):            hi%!d(MISSING)
Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC)
	Printf("%*s", 4.5, "hi"):  %!(BADWIDTH)hi
	Printf("%.*s", 4.5, "hi"): %!(BADPREC)hi
Invalid or invalid use of argument index: %!(BADINDEX)
	Printf("%*[2]d", 7):       %!d(BADINDEX)
	Printf("%.[2]d", 7):       %!d(BADINDEX)
</pre> <p>All errors begin with the string "%!" followed sometimes by a single character (the verb) and end with a parenthesized description. </p>
<p>If an Error or String method triggers a panic when called by a print routine, the fmt package reformats the error message from the panic, decorating it with an indication that it came through the fmt package. For example, if a String method calls panic("bad"), the resulting formatted message will look like </p>
<pre data-language="go">%!s(PANIC=bad)
</pre> <p>The %!s just shows the print verb in use when the failure occurred. If the panic is caused by a nil receiver to an Error or String method, however, the output is the undecorated string, "<nil>". </p>
<h3 id="hdr-Scanning">Scanning</h3> <p>An analogous set of functions scans formatted text to yield values. Scan, Scanf and Scanln read from os.Stdin; Fscan, Fscanf and Fscanln read from a specified io.Reader; Sscan, Sscanf and Sscanln read from an argument string. </p>
<p>Scan, Fscan, Sscan treat newlines in the input as spaces. </p>
<p>Scanln, Fscanln and Sscanln stop scanning at a newline and require that the items be followed by a newline or EOF. </p>
<p>Scanf, Fscanf, and Sscanf parse the arguments according to a format string, analogous to that of Printf. In the text that follows, 'space' means any Unicode whitespace character except newline. </p>
<p>In the format string, a verb introduced by the % character consumes and parses input; these verbs are described in more detail below. A character other than %, space, or newline in the format consumes exactly that input character, which must be present. A newline with zero or more spaces before it in the format string consumes zero or more spaces in the input followed by a single newline or the end of the input. A space following a newline in the format string consumes zero or more spaces in the input. Otherwise, any run of one or more spaces in the format string consumes as many spaces as possible in the input. Unless the run of spaces in the format string appears adjacent to a newline, the run must consume at least one space from the input or find the end of the input. </p>
<p>The handling of spaces and newlines differs from that of C's scanf family: in C, newlines are treated as any other space, and it is never an error when a run of spaces in the format string finds no spaces to consume in the input. </p>
<p>The verbs behave analogously to those of Printf. For example, %x will scan an integer as a hexadecimal number, and %v will scan the default representation format for the value. The Printf verbs %p and %T and the flags # and + are not implemented. For floating-point and complex values, all valid formatting verbs (%b %e %E %f %F %g %G %x %X and %v) are equivalent and accept both decimal and hexadecimal notation (for example: "2.3e+7", "0x4.5p-8") and digit-separating underscores (for example: "3.14159_26535_89793"). </p>
<p>Input processed by verbs is implicitly space-delimited: the implementation of every verb except %c starts by discarding leading spaces from the remaining input, and the %s verb (and %v reading into a string) stops consuming input at the first space or newline character. </p>
<p>The familiar base-setting prefixes 0b (binary), 0o and 0 (octal), and 0x (hexadecimal) are accepted when scanning integers without a format or with the %v verb, as are digit-separating underscores. </p>
<p>Width is interpreted in the input text but there is no syntax for scanning with a precision (no %5.2f, just %5f). If width is provided, it applies after leading spaces are trimmed and specifies the maximum number of runes to read to satisfy the verb. For example, </p>
<pre data-language="go">Sscanf(" 1234567 ", "%5s%d", &s, &i)
</pre> <p>will set s to "12345" and i to 67 while </p>
<pre data-language="go">Sscanf(" 12 34 567 ", "%5s%d", &s, &i)
</pre> <p>will set s to "12" and i to 34. </p>
<p>In all the scanning functions, a carriage return followed immediately by a newline is treated as a plain newline (\r\n means the same as \n). </p>
<p>In all the scanning functions, if an operand implements method Scan (that is, it implements the Scanner interface) that method will be used to scan the text for that operand. Also, if the number of arguments scanned is less than the number of arguments provided, an error is returned. </p>
<p>All arguments to be scanned must be either pointers to basic types or implementations of the Scanner interface. </p>
<p>Like Scanf and Fscanf, Sscanf need not consume its entire input. There is no way to recover how much of the input string Sscanf used. </p>
<p>Note: Fscan etc. can read one character (rune) past the input they return, which means that a loop calling a scan routine may skip some of the input. This is usually a problem only when there is no space between input values. If the reader provided to Fscan implements ReadRune, that method will be used to read characters. If the reader also implements UnreadRune, that method will be used to save the character and successive calls will not lose data. To attach ReadRune and UnreadRune methods to a reader without that capability, use bufio.NewReader. </p>   <h4 id="example__formats"> <span class="text">Example (Formats)</span>
</h4> <p>These examples demonstrate the basics of printing using a format string. Printf, Sprintf, and Fprintf all take a format string that specifies how to format the subsequent arguments. For example, %d (we call that a 'verb') says to print the corresponding argument, which must be an integer (or something containing an integer, such as a slice of ints) in decimal. The verb %v ('v' for 'value') always formats the argument in its default form, just how Print or Println would show it. The special verb %T ('T' for 'Type') prints the type of the argument rather than its value. The examples are not exhaustive; see the package comment for all the details. </p> <p>Code:</p> <pre class="code" data-language="go">// A basic set of examples showing that %v is the default format, in this
// case decimal for integers, which can be explicitly requested with %d;
// the output is just what Println generates.
integer := 23
// Each of these prints "23" (without the quotes).
fmt.Println(integer)
fmt.Printf("%v\n", integer)
fmt.Printf("%d\n", integer)
// The special verb %T shows the type of an item rather than its value.
fmt.Printf("%T %T\n", integer, &integer)
// Result: int *int
// Println(x) is the same as Printf("%v\n", x) so we will use only Printf
// in the following examples. Each one demonstrates how to format values of
// a particular type, such as integers or strings. We start each format
// string with %v to show the default output and follow that with one or
// more custom formats.
// Booleans print as "true" or "false" with %v or %t.
truth := true
fmt.Printf("%v %t\n", truth, truth)
// Result: true true
// Integers print as decimals with %v and %d,
// or in hex with %x, octal with %o, or binary with %b.
answer := 42
fmt.Printf("%v %d %x %o %b\n", answer, answer, answer, answer, answer)
// Result: 42 42 2a 52 101010
// Floats have multiple formats: %v and %g print a compact representation,
// while %f prints a decimal point and %e uses exponential notation. The
// format %6.2f used here shows how to set the width and precision to
// control the appearance of a floating-point value. In this instance, 6 is
// the total width of the printed text for the value (note the extra spaces
// in the output) and 2 is the number of decimal places to show.
pi := math.Pi
fmt.Printf("%v %g %.2f (%6.2f) %e\n", pi, pi, pi, pi, pi)
// Result: 3.141592653589793 3.141592653589793 3.14 (  3.14) 3.141593e+00
// Complex numbers format as parenthesized pairs of floats, with an 'i'
// after the imaginary part.
point := 110.7 + 22.5i
fmt.Printf("%v %g %.2f %.2e\n", point, point, point, point)
// Result: (110.7+22.5i) (110.7+22.5i) (110.70+22.50i) (1.11e+02+2.25e+01i)
// Runes are integers but when printed with %c show the character with that
// Unicode value. The %q verb shows them as quoted characters, %U as a
// hex Unicode code point, and %#U as both a code point and a quoted
// printable form if the rune is printable.
smile := 'π'
fmt.Printf("%v %d %c %q %U %#U\n", smile, smile, smile, smile, smile, smile)
// Result: 128512 128512 π 'π' U+1F600 U+1F600 'π'
// Strings are formatted with %v and %s as-is, with %q as quoted strings,
// and %#q as backquoted strings.
placeholders := `foo "bar"`
fmt.Printf("%v %s %q %#q\n", placeholders, placeholders, placeholders, placeholders)
// Result: foo "bar" foo "bar" "foo \"bar\"" `foo "bar"`
// Maps formatted with %v show keys and values in their default formats.
// The %#v form (the # is called a "flag" in this context) shows the map in
// the Go source format. Maps are printed in a consistent order, sorted
// by the values of the keys.
isLegume := map[string]bool{
    "peanut":    true,
    "dachshund": false,
}
fmt.Printf("%v %#v\n", isLegume, isLegume)
// Result: map[dachshund:false peanut:true] map[string]bool{"dachshund":false, "peanut":true}
// Structs formatted with %v show field values in their default formats.
// The %+v form shows the fields by name, while %#v formats the struct in
// Go source format.
person := struct {
    Name string
    Age  int
}{"Kim", 22}
fmt.Printf("%v %+v %#v\n", person, person, person)
// Result: {Kim 22} {Name:Kim Age:22} struct { Name string; Age int }{Name:"Kim", Age:22}
// The default format for a pointer shows the underlying value preceded by
// an ampersand. The %p verb prints the pointer value in hex. We use a
// typed nil for the argument to %p here because the value of any non-nil
// pointer would change from run to run; run the commented-out Printf
// call yourself to see.
pointer := &person
fmt.Printf("%v %p\n", pointer, (*int)(nil))
// Result: &{Kim 22} 0x0
// fmt.Printf("%v %p\n", pointer, pointer)
// Result: &{Kim 22} 0x010203 // See comment above.
// Arrays and slices are formatted by applying the format to each element.
greats := [5]string{"Kitano", "Kobayashi", "Kurosawa", "Miyazaki", "Ozu"}
fmt.Printf("%v %q\n", greats, greats)
// Result: [Kitano Kobayashi Kurosawa Miyazaki Ozu] ["Kitano" "Kobayashi" "Kurosawa" "Miyazaki" "Ozu"]
kGreats := greats[:3]
fmt.Printf("%v %q %#v\n", kGreats, kGreats, kGreats)
// Result: [Kitano Kobayashi Kurosawa] ["Kitano" "Kobayashi" "Kurosawa"] []string{"Kitano", "Kobayashi", "Kurosawa"}
// Byte slices are special. Integer verbs like %d print the elements in
// that format. The %s and %q forms treat the slice like a string. The %x
// verb has a special form with the space flag that puts a space between
// the bytes.
cmd := []byte("aβ")
fmt.Printf("%v %d %s %q %x % x\n", cmd, cmd, cmd, cmd, cmd, cmd)
// Result: [97 226 140 152] [97 226 140 152] aβ "aβ" 61e28c98 61 e2 8c 98
// Types that implement Stringer are printed the same as strings. Because
// Stringers return a string, we can print them using a string-specific
// verb such as %q.
now := time.Unix(123456789, 0).UTC() // time.Time implements fmt.Stringer.
fmt.Printf("%v %q\n", now, now)
// Result: 1973-11-29 21:33:09 +0000 UTC "1973-11-29 21:33:09 +0000 UTC"
</pre> <p>Output:</p> <pre class="output" data-language="go">23
23
23
int *int
true true
42 42 2a 52 101010
3.141592653589793 3.141592653589793 3.14 (  3.14) 3.141593e+00
(110.7+22.5i) (110.7+22.5i) (110.70+22.50i) (1.11e+02+2.25e+01i)
128512 128512 π 'π' U+1F600 U+1F600 'π'
foo "bar" foo "bar" "foo \"bar\"" `foo "bar"`
map[dachshund:false peanut:true] map[string]bool{"dachshund":false, "peanut":true}
{Kim 22} {Name:Kim Age:22} struct { Name string; Age int }{Name:"Kim", Age:22}
&{Kim 22} 0x0
[Kitano Kobayashi Kurosawa Miyazaki Ozu] ["Kitano" "Kobayashi" "Kurosawa" "Miyazaki" "Ozu"]
[Kitano Kobayashi Kurosawa] ["Kitano" "Kobayashi" "Kurosawa"] []string{"Kitano", "Kobayashi", "Kurosawa"}
[97 226 140 152] [97 226 140 152] aβ "aβ" 61e28c98 61 e2 8c 98
1973-11-29 21:33:09 +0000 UTC "1973-11-29 21:33:09 +0000 UTC"
</pre>      <h4 id="example__printers"> <span class="text">Example (Printers)</span>
</h4> <p>Print, Println, and Printf lay out their arguments differently. In this example we can compare their behaviors. Println always adds blanks between the items it prints, while Print adds blanks only between non-string arguments and Printf does exactly what it is told. Sprint, Sprintln, Sprintf, Fprint, Fprintln, and Fprintf behave the same as their corresponding Print, Println, and Printf functions shown here. </p> <p>Code:</p> <pre class="code" data-language="go">a, b := 3.0, 4.0
h := math.Hypot(a, b)
// Print inserts blanks between arguments when neither is a string.
// It does not add a newline to the output, so we add one explicitly.
fmt.Print("The vector (", a, b, ") has length ", h, ".\n")
// Println always inserts spaces between its arguments,
// so it cannot be used to produce the same output as Print in this case;
// its output has extra spaces.
// Also, Println always adds a newline to the output.
fmt.Println("The vector (", a, b, ") has length", h, ".")
// Printf provides complete control but is more complex to use.
// It does not add a newline to the output, so we add one explicitly
// at the end of the format specifier string.
fmt.Printf("The vector (%g %g) has length %g.\n", a, b, h)
</pre> <p>Output:</p> <pre class="output" data-language="go">The vector (3 4) has length 5.
The vector ( 3 4 ) has length 5 .
The vector (3 4) has length 5.
</pre>        <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#Append">func Append(b []byte, a ...any) []byte</a></li>
<li><a href="#Appendf">func Appendf(b []byte, format string, a ...any) []byte</a></li>
<li><a href="#Appendln">func Appendln(b []byte, a ...any) []byte</a></li>
<li><a href="#Errorf">func Errorf(format string, a ...any) error</a></li>
<li><a href="#FormatString">func FormatString(state State, verb rune) string</a></li>
<li><a href="#Fprint">func Fprint(w io.Writer, a ...any) (n int, err error)</a></li>
<li><a href="#Fprintf">func Fprintf(w io.Writer, format string, a ...any) (n int, err error)</a></li>
<li><a href="#Fprintln">func Fprintln(w io.Writer, a ...any) (n int, err error)</a></li>
<li><a href="#Fscan">func Fscan(r io.Reader, a ...any) (n int, err error)</a></li>
<li><a href="#Fscanf">func Fscanf(r io.Reader, format string, a ...any) (n int, err error)</a></li>
<li><a href="#Fscanln">func Fscanln(r io.Reader, a ...any) (n int, err error)</a></li>
<li><a href="#Print">func Print(a ...any) (n int, err error)</a></li>
<li><a href="#Printf">func Printf(format string, a ...any) (n int, err error)</a></li>
<li><a href="#Println">func Println(a ...any) (n int, err error)</a></li>
<li><a href="#Scan">func Scan(a ...any) (n int, err error)</a></li>
<li><a href="#Scanf">func Scanf(format string, a ...any) (n int, err error)</a></li>
<li><a href="#Scanln">func Scanln(a ...any) (n int, err error)</a></li>
<li><a href="#Sprint">func Sprint(a ...any) string</a></li>
<li><a href="#Sprintf">func Sprintf(format string, a ...any) string</a></li>
<li><a href="#Sprintln">func Sprintln(a ...any) string</a></li>
<li><a href="#Sscan">func Sscan(str string, a ...any) (n int, err error)</a></li>
<li><a href="#Sscanf">func Sscanf(str string, format string, a ...any) (n int, err error)</a></li>
<li><a href="#Sscanln">func Sscanln(str string, a ...any) (n int, err error)</a></li>
<li><a href="#Formatter">type Formatter</a></li>
<li><a href="#GoStringer">type GoStringer</a></li>
<li><a href="#ScanState">type ScanState</a></li>
<li><a href="#Scanner">type Scanner</a></li>
<li><a href="#State">type State</a></li>
<li><a href="#Stringer">type Stringer</a></li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_Errorf">Errorf</a></dd> <dd><a class="exampleLink" href="#example_Fprint">Fprint</a></dd> <dd><a class="exampleLink" href="#example_Fprintf">Fprintf</a></dd> <dd><a class="exampleLink" href="#example_Fprintln">Fprintln</a></dd> <dd><a class="exampleLink" href="#example_Fscanf">Fscanf</a></dd> <dd><a class="exampleLink" href="#example_Fscanln">Fscanln</a></dd> <dd><a class="exampleLink" href="#example_GoStringer">GoStringer</a></dd> <dd><a class="exampleLink" href="#example_Print">Print</a></dd> <dd><a class="exampleLink" href="#example_Printf">Printf</a></dd> <dd><a class="exampleLink" href="#example_Println">Println</a></dd> <dd><a class="exampleLink" href="#example_Sprint">Sprint</a></dd> <dd><a class="exampleLink" href="#example_Sprintf">Sprintf</a></dd> <dd><a class="exampleLink" href="#example_Sprintln">Sprintln</a></dd> <dd><a class="exampleLink" href="#example_Sscanf">Sscanf</a></dd> <dd><a class="exampleLink" href="#example_Stringer">Stringer</a></dd> <dd><a class="exampleLink" href="#example__formats">Package (Formats)</a></dd> <dd><a class="exampleLink" href="#example__printers">Package (Printers)</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>doc.go</span> <span>errors.go</span> <span>format.go</span> <span>print.go</span> <span>scan.go</span>  </p>   <h2 id="Append">func <span>Append</span>  <span title="Added in Go 1.19">1.19</span> </h2> <pre data-language="go">func Append(b []byte, a ...any) []byte</pre> <p>Append formats using the default formats for its operands, appends the result to the byte slice, and returns the updated slice. </p>
<h2 id="Appendf">func <span>Appendf</span>  <span title="Added in Go 1.19">1.19</span> </h2> <pre data-language="go">func Appendf(b []byte, format string, a ...any) []byte</pre> <p>Appendf formats according to a format specifier, appends the result to the byte slice, and returns the updated slice. </p>
<h2 id="Appendln">func <span>Appendln</span>  <span title="Added in Go 1.19">1.19</span> </h2> <pre data-language="go">func Appendln(b []byte, a ...any) []byte</pre> <p>Appendln formats using the default formats for its operands, appends the result to the byte slice, and returns the updated slice. Spaces are always added between operands and a newline is appended. </p>
<h2 id="Errorf">func <span>Errorf</span>  </h2> <pre data-language="go">func Errorf(format string, a ...any) error</pre> <p>Errorf formats according to a format specifier and returns the string as a value that satisfies error. </p>
<p>If the format specifier includes a %w verb with an error operand, the returned error will implement an Unwrap method returning the operand. If there is more than one %w verb, the returned error will implement an Unwrap method returning a []error containing all the %w operands in the order they appear in the arguments. It is invalid to supply the %w verb with an operand that does not implement the error interface. The %w verb is otherwise a synonym for %v. </p>   <h4 id="example_Errorf"> <span class="text">Example</span>
</h4> <p>The Errorf function lets us use formatting features to create descriptive error messages. </p> <p>Code:</p> <pre class="code" data-language="go">const name, id = "bueller", 17
err := fmt.Errorf("user %q (id %d) not found", name, id)
fmt.Println(err.Error())
</pre> <p>Output:</p> <pre class="output" data-language="go">user "bueller" (id 17) not found
</pre>   <h2 id="FormatString">func <span>FormatString</span>  <span title="Added in Go 1.20">1.20</span> </h2> <pre data-language="go">func FormatString(state State, verb rune) string</pre> <p>FormatString returns a string representing the fully qualified formatting directive captured by the State, followed by the argument verb. (State does not itself contain the verb.) The result has a leading percent sign followed by any flags, the width, and the precision. Missing flags, width, and precision are omitted. This function allows a Formatter to reconstruct the original directive triggering the call to Format. </p>
<h2 id="Fprint">func <span>Fprint</span>  </h2> <pre data-language="go">func Fprint(w io.Writer, a ...any) (n int, err error)</pre> <p>Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered. </p>   <h4 id="example_Fprint"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
n, err := fmt.Fprint(os.Stdout, name, " is ", age, " years old.\n")
// The n and err return values from Fprint are
// those returned by the underlying io.Writer.
if err != nil {
    fmt.Fprintf(os.Stderr, "Fprint: %v\n", err)
}
fmt.Print(n, " bytes written.\n")
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
21 bytes written.
</pre>   <h2 id="Fprintf">func <span>Fprintf</span>  </h2> <pre data-language="go">func Fprintf(w io.Writer, format string, a ...any) (n int, err error)</pre> <p>Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered. </p>   <h4 id="example_Fprintf"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
n, err := fmt.Fprintf(os.Stdout, "%s is %d years old.\n", name, age)
// The n and err return values from Fprintf are
// those returned by the underlying io.Writer.
if err != nil {
    fmt.Fprintf(os.Stderr, "Fprintf: %v\n", err)
}
fmt.Printf("%d bytes written.\n", n)
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
21 bytes written.
</pre>   <h2 id="Fprintln">func <span>Fprintln</span>  </h2> <pre data-language="go">func Fprintln(w io.Writer, a ...any) (n int, err error)</pre> <p>Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered. </p>   <h4 id="example_Fprintln"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
n, err := fmt.Fprintln(os.Stdout, name, "is", age, "years old.")
// The n and err return values from Fprintln are
// those returned by the underlying io.Writer.
if err != nil {
    fmt.Fprintf(os.Stderr, "Fprintln: %v\n", err)
}
fmt.Println(n, "bytes written.")
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
21 bytes written.
</pre>   <h2 id="Fscan">func <span>Fscan</span>  </h2> <pre data-language="go">func Fscan(r io.Reader, a ...any) (n int, err error)</pre> <p>Fscan scans text read from r, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why. </p>
<h2 id="Fscanf">func <span>Fscanf</span>  </h2> <pre data-language="go">func Fscanf(r io.Reader, format string, a ...any) (n int, err error)</pre> <p>Fscanf scans text read from r, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully parsed. Newlines in the input must match newlines in the format. </p>   <h4 id="example_Fscanf"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">var (
    i int
    b bool
    s string
)
r := strings.NewReader("5 true gophers")
n, err := fmt.Fscanf(r, "%d %t %s", &i, &b, &s)
if err != nil {
    fmt.Fprintf(os.Stderr, "Fscanf: %v\n", err)
}
fmt.Println(i, b, s)
fmt.Println(n)
</pre> <p>Output:</p> <pre class="output" data-language="go">5 true gophers
3
</pre>   <h2 id="Fscanln">func <span>Fscanln</span>  </h2> <pre data-language="go">func Fscanln(r io.Reader, a ...any) (n int, err error)</pre> <p>Fscanln is similar to Fscan, but stops scanning at a newline and after the final item there must be a newline or EOF. </p>   <h4 id="example_Fscanln"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">s := `dmr 1771 1.61803398875
    ken 271828 3.14159`
r := strings.NewReader(s)
var a string
var b int
var c float64
for {
    n, err := fmt.Fscanln(r, &a, &b, &c)
    if err == io.EOF {
        break
    }
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d: %s, %d, %f\n", n, a, b, c)
}
</pre> <p>Output:</p> <pre class="output" data-language="go">3: dmr, 1771, 1.618034
3: ken, 271828, 3.141590
</pre>   <h2 id="Print">func <span>Print</span>  </h2> <pre data-language="go">func Print(a ...any) (n int, err error)</pre> <p>Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered. </p>   <h4 id="example_Print"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
fmt.Print(name, " is ", age, " years old.\n")
// It is conventional not to worry about any
// error returned by Print.
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
</pre>   <h2 id="Printf">func <span>Printf</span>  </h2> <pre data-language="go">func Printf(format string, a ...any) (n int, err error)</pre> <p>Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered. </p>   <h4 id="example_Printf"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
fmt.Printf("%s is %d years old.\n", name, age)
// It is conventional not to worry about any
// error returned by Printf.
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
</pre>   <h2 id="Println">func <span>Println</span>  </h2> <pre data-language="go">func Println(a ...any) (n int, err error)</pre> <p>Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered. </p>   <h4 id="example_Println"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
fmt.Println(name, "is", age, "years old.")
// It is conventional not to worry about any
// error returned by Println.
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
</pre>   <h2 id="Scan">func <span>Scan</span>  </h2> <pre data-language="go">func Scan(a ...any) (n int, err error)</pre> <p>Scan scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why. </p>
<h2 id="Scanf">func <span>Scanf</span>  </h2> <pre data-language="go">func Scanf(format string, a ...any) (n int, err error)</pre> <p>Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why. Newlines in the input must match newlines in the format. The one exception: the verb %c always scans the next rune in the input, even if it is a space (or tab etc.) or newline. </p>
<h2 id="Scanln">func <span>Scanln</span>  </h2> <pre data-language="go">func Scanln(a ...any) (n int, err error)</pre> <p>Scanln is similar to Scan, but stops scanning at a newline and after the final item there must be a newline or EOF. </p>
<h2 id="Sprint">func <span>Sprint</span>  </h2> <pre data-language="go">func Sprint(a ...any) string</pre> <p>Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string. </p>   <h4 id="example_Sprint"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
s := fmt.Sprint(name, " is ", age, " years old.\n")
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
</pre>   <h2 id="Sprintf">func <span>Sprintf</span>  </h2> <pre data-language="go">func Sprintf(format string, a ...any) string</pre> <p>Sprintf formats according to a format specifier and returns the resulting string. </p>   <h4 id="example_Sprintf"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
s := fmt.Sprintf("%s is %d years old.\n", name, age)
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
</pre>   <h2 id="Sprintln">func <span>Sprintln</span>  </h2> <pre data-language="go">func Sprintln(a ...any) string</pre> <p>Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended. </p>   <h4 id="example_Sprintln"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">const name, age = "Kim", 22
s := fmt.Sprintln(name, "is", age, "years old.")
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
</pre> <p>Output:</p> <pre class="output" data-language="go">Kim is 22 years old.
</pre>   <h2 id="Sscan">func <span>Sscan</span>  </h2> <pre data-language="go">func Sscan(str string, a ...any) (n int, err error)</pre> <p>Sscan scans the argument string, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why. </p>
<h2 id="Sscanf">func <span>Sscanf</span>  </h2> <pre data-language="go">func Sscanf(str string, format string, a ...any) (n int, err error)</pre> <p>Sscanf scans the argument string, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully parsed. Newlines in the input must match newlines in the format. </p>   <h4 id="example_Sscanf"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">var name string
var age int
n, err := fmt.Sscanf("Kim is 22 years old", "%s is %d years old", &name, &age)
if err != nil {
    panic(err)
}
fmt.Printf("%d: %s, %d\n", n, name, age)
</pre> <p>Output:</p> <pre class="output" data-language="go">2: Kim, 22
</pre>   <h2 id="Sscanln">func <span>Sscanln</span>  </h2> <pre data-language="go">func Sscanln(str string, a ...any) (n int, err error)</pre> <p>Sscanln is similar to Sscan, but stops scanning at a newline and after the final item there must be a newline or EOF. </p>
<h2 id="Formatter">type <span>Formatter</span>  </h2> <p>Formatter is implemented by any value that has a Format method. The implementation controls how State and rune are interpreted, and may call Sprint() or Fprint(f) etc. to generate its output. </p>
<pre data-language="go">type Formatter interface {
    Format(f State, verb rune)
}</pre> <h2 id="GoStringer">type <span>GoStringer</span>  </h2> <p>GoStringer is implemented by any value that has a GoString method, which defines the Go syntax for that value. The GoString method is used to print values passed as an operand to a %#v format. </p>
<pre data-language="go">type GoStringer interface {
    GoString() string
}</pre>    <h4 id="example_GoStringer"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">package fmt_test
import (
    "fmt"
)
// Address has a City, State and a Country.
type Address struct {
    City    string
    State   string
    Country string
}
// Person has a Name, Age and Address.
type Person struct {
    Name string
    Age  uint
    Addr *Address
}
// GoString makes Person satisfy the GoStringer interface.
// The return value is valid Go code that can be used to reproduce the Person struct.
func (p Person) GoString() string {
    if p.Addr != nil {
        return fmt.Sprintf("Person{Name: %q, Age: %d, Addr: &Address{City: %q, State: %q, Country: %q}}", p.Name, int(p.Age), p.Addr.City, p.Addr.State, p.Addr.Country)
    }
    return fmt.Sprintf("Person{Name: %q, Age: %d}", p.Name, int(p.Age))
}
func ExampleGoStringer() {
    p1 := Person{
        Name: "Warren",
        Age:  31,
        Addr: &Address{
            City:    "Denver",
            State:   "CO",
            Country: "U.S.A.",
        },
    }
    // If GoString() wasn't implemented, the output of `fmt.Printf("%#v", p1)` would be similar to
    // Person{Name:"Warren", Age:0x1f, Addr:(*main.Address)(0x10448240)}
    fmt.Printf("%#v\n", p1)
    p2 := Person{
        Name: "Theia",
        Age:  4,
    }
    // If GoString() wasn't implemented, the output of `fmt.Printf("%#v", p2)` would be similar to
    // Person{Name:"Theia", Age:0x4, Addr:(*main.Address)(nil)}
    fmt.Printf("%#v\n", p2)
    // Output:
    // Person{Name: "Warren", Age: 31, Addr: &Address{City: "Denver", State: "CO", Country: "U.S.A."}}
    // Person{Name: "Theia", Age: 4}
}
</pre>   <h2 id="ScanState">type <span>ScanState</span>  </h2> <p>ScanState represents the scanner state passed to custom scanners. Scanners may do rune-at-a-time scanning or ask the ScanState to discover the next space-delimited token. </p>
<pre data-language="go">type ScanState interface {
    // ReadRune reads the next rune (Unicode code point) from the input.
    // If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
    // return EOF after returning the first '\n' or when reading beyond
    // the specified width.
    ReadRune() (r rune, size int, err error)
    // UnreadRune causes the next call to ReadRune to return the same rune.
    UnreadRune() error
    // SkipSpace skips space in the input. Newlines are treated appropriately
    // for the operation being performed; see the package documentation
    // for more information.
    SkipSpace()
    // Token skips space in the input if skipSpace is true, then returns the
    // run of Unicode code points c satisfying f(c).  If f is nil,
    // !unicode.IsSpace(c) is used; that is, the token will hold non-space
    // characters. Newlines are treated appropriately for the operation being
    // performed; see the package documentation for more information.
    // The returned slice points to shared data that may be overwritten
    // by the next call to Token, a call to a Scan function using the ScanState
    // as input, or when the calling Scan method returns.
    Token(skipSpace bool, f func(rune) bool) (token []byte, err error)
    // Width returns the value of the width option and whether it has been set.
    // The unit is Unicode code points.
    Width() (wid int, ok bool)
    // Because ReadRune is implemented by the interface, Read should never be
    // called by the scanning routines and a valid implementation of
    // ScanState may choose always to return an error from Read.
    Read(buf []byte) (n int, err error)
}</pre> <h2 id="Scanner">type <span>Scanner</span>  </h2> <p>Scanner is implemented by any value that has a Scan method, which scans the input for the representation of a value and stores the result in the receiver, which must be a pointer to be useful. The Scan method is called for any argument to Scan, Scanf, or Scanln that implements it. </p>
<pre data-language="go">type Scanner interface {
    Scan(state ScanState, verb rune) error
}</pre> <h2 id="State">type <span>State</span>  </h2> <p>State represents the printer state passed to custom formatters. It provides access to the io.Writer interface plus information about the flags and options for the operand's format specifier. </p>
<pre data-language="go">type State interface {
    // Write is the function to call to emit formatted output to be printed.
    Write(b []byte) (n int, err error)
    // Width returns the value of the width option and whether it has been set.
    Width() (wid int, ok bool)
    // Precision returns the value of the precision option and whether it has been set.
    Precision() (prec int, ok bool)
    // Flag reports whether the flag c, a character, has been set.
    Flag(c int) bool
}</pre> <h2 id="Stringer">type <span>Stringer</span>  </h2> <p>Stringer is implemented by any value that has a String method, which defines the βnativeβ format for that value. The String method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as Print. </p>
<pre data-language="go">type Stringer interface {
    String() string
}</pre>    <h4 id="example_Stringer"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">package fmt_test
import (
    "fmt"
)
// Animal has a Name and an Age to represent an animal.
type Animal struct {
    Name string
    Age  uint
}
// String makes Animal satisfy the Stringer interface.
func (a Animal) String() string {
    return fmt.Sprintf("%v (%d)", a.Name, a.Age)
}
func ExampleStringer() {
    a := Animal{
        Name: "Gopher",
        Age:  2,
    }
    fmt.Println(a)
    // Output: Gopher (2)
}
</pre><div class="_attribution">
  <p class="_attribution-p">
    © Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/fmt/" class="_attribution-link">http://golang.org/pkg/fmt/</a>
  </p>
</div>
 |