Golang FMT format "placeholder"

Source: Internet
Author: User

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

# define sample types and variables  Humanstruct{name string}=Human{name:" Zhangsan "}            

Common placeholderPlaceholder     Description Example Output%VThe default format for the corresponding value. Printf("%v",People) {Zhangsan},%+VField names are added when the structure is printed Printf("%+v",People) {Name:zhangsan}% The go syntax #v the corresponding value represents Printf ("#v", people) main. Human{name: "Zhangsan"}%t  go syntax means  printf (  "%T" , People  main< Span class= "pun". human%%  literally percent-semicolon, Not a placeholder for a value  printf ( "percent"  %            
The boolean placeholder placeholder          Describes an example of  output %true or false. Printf("%t",true)true          
Integer placeholdersPlaceholder     Description Example Output%BBinary representation Printf("%b", 5) 101%CCorrespondingUnicodeThe character represented by the code point Printf("%c", 0x4e2d) In%DDecimal representation Printf("%d", 0x12) 18%OOctal representation Printf("%d", 10) 12%QSingle quotation marks around the character literal, by theGoThe syntax is safe to escape Printf("%q", 0x4e2d) In%XHexadecimal representation, alphabetic form lowercaseA-FPrintf("%x", 13)D%x  hexadecimal notation, uppercase  A-< Span class= "PLN" >f printf ( "%x"  13)  D% u unicode format: u+1234  "u+%04x" printf ( "%u" , 0x4e2d  U+4e2d    
Components of floating-point numbers and complex numbers (real and imaginary parts)Placeholder     Description Example Output%BThe scientific notation of the power of exponent two without fractional parts, AndStrConv.Formatfloat Of ' B ' The conversion format is consistent. For example -123456p-78%EScientific counting methods, such as -1234.456e+78 Printf("%e", 10.2) 1.020000e+01%EScientific counting methods, such as -1234.456E+78 Printf("%e", 10.2) 1.020000E+01%FHave a decimal point but no index, for example 123.456 Printf("%f", 10.2) 10.200000%GChoose according to the situation %EOr %FTo produce a more compact (no end-0) output Printf("%g", 10.20)   10.2%g  select %e  or  %f Span class= "pun" to produce more compact (no end of 0) output  printf  ( "%G" , 10.20 +2i)   ( 10.2+2i)        
String and byte slicesPlaceholder     Description Example Output%SThe output string representation (StringType or []Byte) Printf('%s ', []Byte("Go Language")) GoLanguage%QA string surrounded by double quotes, by theGoThe syntax is safe to escape Printf("%q",  "Go language" )  %x  hex, small letter, two characters per byte  printf ( "%x"   "Golang" )   676f6c616e67%x  hex, capital letter, two characters per byte  printf ( "%x"   "Golang" )   676f6c616e67                 
Pointer  placeholder          Description example output % hexadecimal representation, prefix 0xPrintf("%p",& people)0x4f57f0               
Other marksPlaceholder      Description Example Output+ The positive or negative number of the total printed value;Q(%+Q) Ensure that only the outputAsciiThe encoded character. Printf("%+q", Chinese) "\u4e2d\u6587"- Padding space on the right instead of the left (the area is left aligned)# Alternate Format: Add leading 0 (% #o) Printf ("% #U", "medium") for octal u+4e2d Add a leading for hex 0x(%#x) or 0X (% #X), remove the leading 0x for%p (% #p); 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  unicode Span class= "pun" > encoded form (such as character  x  U+0078  ' x ' ).  "  (space) leave blank for the omitted sign in the numeric value (%  D  with 16 binary (% X, % X) when you print a string or slice, spaces are separated by a space between the bytes. Span class= "PLN" >0  padded leading 0             

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.