Golang FMT format "placeholder"

Source: Internet
Author: User
Tags compact
This is a creation in Article, where the information may have evolved or changed.

Golang's FMT package implements formatted I/O functions, similar to the C printf and scanf.

# define sample types and variables type Human struct {    Name string}var people = human{name: "Zhangsan"}

The generic placeholder placeholder     describes      the default format for an example output%v corresponding value.            Printf ("%v", people)   {ZHANGSAN},%+V The     structure is printed, the field name     printf ("%+v", people)  {name:zhangsan}% #v     the corresponding value of the go syntax represents            printf ( "#v", people)   main. Human{name: "Zhangsan"}%t the      corresponding value of the type of Go syntax represents       Printf ("%T", people)   main. human%%      literal percent semicolon, not a placeholder for value  Printf ("percent")            %
The Boolean placeholder placeholder       Describes an                example output of                     %t          true or false.     Printf ("%t", true)       true
 Integer Placeholder Placeholder Description example output%b binary representation                             printf ("%b", 5) 101%c the characters represented by the corresponding Unicode code point printf ("%c", 0x4e2d) in%d decimal notation      Printf ("%d", 0x12) 18%o        Octal means printf ("%d", 12%q) the character literal surrounded by single quotes, safely escaped by Go syntax printf ("%q", 0x4e2d)             ' In '%x hexadecimal means lowercase a-f printf ("%x", d%x hexadecimal), alphabetic form uppercase A-f printf ("%x", 13) d%u Unicode format: u+1234, equivalent to "u+%04x" Printf ("%u", 0x4e2d) u+4e2d 
The floating-point and complex parts (both real and imaginary) placeholder     descriptions illustrate the            output of%b with      no fractional parts, exponential two of the scientific notation of power,        and StrConv. The formatfloat ' B ' conversion format is consistent. For example -123456p-78%e      scientific notation, for example -1234.456e+78        Printf ("%e", 10.2)     1.020000e+01%e      scientific notation, for example -1234.456E+78        printf ("%e", 10.2)     1.020000e+01%f      has a decimal point but no exponent, such as 123.456        printf ("%f", 10.2)     10.200000%g      Select%e or%f to produce a more compact (no end 0) output printf ("%g", 10.20) 10.2%g according to the situation      select%e or%f to produce a more compact (no end 0) output printf ("%g", 10 .20+2i) (10.2+2i)
string and byte slice placeholder     Description                              example                           output%s      output string representation (string type or []byte)   Printf ("%s", []byte ("Go Language"))  Go language %q      double quotes around the string, safely escaped by Go syntax  printf ("%q", "Go Language")         "Go language"%x      16, lowercase letters, two characters per byte      printf ("%x", " Golang ")         676f6c616e67%x      16 binary, uppercase letters, two characters per byte      Printf ("%x "," Golang ")         676f6c616e67
Pointer placeholder         Description                      example                             output%p      hexadecimal representation, prefix 0x          Printf ("%p", &people)             0x4f57f0
Other token placeholders      illustrate                             examples          of output +      the positive and negative sign of total printed values, and for%q (%+Q), only ASCII encoded characters are guaranteed to be output.                                            printf ("%+q", "Chinese")  "\u4e2d\u6587"-      padding space on the right instead of the left (left-aligned to the area) #      Alternate format: Add a leading 0 (% #o)      Printf for octal (" % #U, ' Medium ')      u+4e2d       Add a leading 0x (% #x) or 0X (% #X) for Hex, minus the leading 0x for%p (% #p),       and if possible,%q (% #q) prints the original (that is, the inverse of the quotes around) string;       if it is a printable character,%u (% #U) writes out the character's       The Unicode encoding form (such as the character X will be printed as u+0078 ' X '). "    (space) to leave blank (% d) for the negative sign omitted in the numeric value;       when a string or slice is printed in 16 binary (% x,% x), the leading 0 is filled with a space between the bytes and      not a space, and for numbers, this moves the fill to the positive sign

Golang does not have a '%u ' dot, and if the integer is an unsigned type, it will be printed as unsigned by default.

The control format for width and precision is in Unicode code point units. The width is the minimum width of the area occupied by the value, and the precision is the number of digits after the decimal point.
When the operand is of type int, the width and precision are indicated by the character ' * '.

For%g/%g, the precision is the total number of all numbers, for example: 123.45,%.4G will print 123.5 (and%6.2f will print 123.45).

The default accuracy of%e and%f is 6

For most numeric types, the width is the minimum number of characters for the output and, if necessary, fills the formatted form with spaces.

In the case of a string type, the precision is the maximum number of characters to output and is truncated if necessary.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.