| Go offers excellent support for string formatting in
the printftradition. Here are some examples of
common string formatting tasks. |  | 
        
        
          |  |   | 
        
        
          |  |  | 
        
        
          |  | type point struct {
	x, y int
}
 | 
        
        
          |  |  | 
        
        
          | Go offers several printing “verbs” designed to
format general Go values. For example, this prints
an instance of our pointstruct. | 	p := point{1, 2}
	fmt.Printf("%v\n", p)
 | 
        
        
          | If the value is a struct, the %+vvariant will
include the struct’s field names. |  | 
        
        
          | The %#vvariant prints a Go syntax representation
of the value, i.e. the source code snippet that
would produce that value. |  | 
        
        
          | To print the type of a value, use %T. |  | 
        
        
          | Formatting booleans is straight-forward. |  | 
        
        
          | There are many options for formatting integers.
Use %dfor standard, base-10 formatting. |  | 
        
        
          | This prints a binary representation. |  | 
        
        
          | This prints the character corresponding to the
given integer. |  | 
        
        
          | %xprovides hex encoding.
 |  | 
        
        
          | There are also several formatting options for
floats. For basic decimal formatting use %f. |  | 
        
        
          | %eand%Eformat the float in (slightly
different versions of) scientific notation.
 | 	fmt.Printf("%e\n", 123400000.0)
	fmt.Printf("%E\n", 123400000.0)
 | 
        
        
          | For basic string printing use %s. | 	fmt.Printf("%s\n", "\"string\"")
 | 
        
        
          | To double-quote strings as in Go source, use %q. | 	fmt.Printf("%q\n", "\"string\"")
 | 
        
        
          | As with integers as seen earlier, %xrenders
the string in base-16, with two output characters
per byte of input. | 	fmt.Printf("%x\n", "hex this")
	fmt.Printf("% x\n", "hex with space")
 | 
        
        
          | To print a representation of a pointer, use %p. |  | 
        
        
          | When formatting numbers you will often want to
control the width and precision of the resulting
figure. To specify the width of an integer, use a
number after the %in the verb. By default the
result will be right-justified and padded with
spaces. | 	fmt.Printf("|%6d|%6d|\n", 12, 345)
 | 
        
        
          | You can also specify the width of printed floats,
though usually you’ll also want to restrict the
decimal precision at the same time with the
width.precision syntax. | 	fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)
 | 
        
        
          | To left-justify, use the -flag. | 	fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)
 | 
        
        
          | You may also want to control width when formatting
strings, especially to ensure that they align in
table-like output. For basic right-justified width. | 	fmt.Printf("|%6s|%6s|\n", "foo", "b")
 | 
        
        
          | To left-justify use the -flag as with numbers. | 	fmt.Printf("|%-6s|%-6s|\n", "foo", "b")
 | 
        
        
          | So far we’ve seen Printf, which prints the
formatted string toos.Stdout.Sprintfformats
and returns a string without printing it anywhere. | 	s := fmt.Sprintf("a %s", "string")
	fmt.Println(s)
 | 
        
        
          | You can format+print to io.Writersother thanos.StdoutusingFprintf. | 	fmt.Fprintf(os.Stderr, "an %s\n", "error")
}
 |