<15> Go string_formatting

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

String formatted output

 PackageMainImport "FMT"Import "OS"typePointstruct{x, Yint}funcMain () {//Create a point structP: = Point{1,2}//Output struct value {1 2}Fmt. Printf ("%v\n", p)//Output struct value and field {x:1 Y:2}Fmt. Printf ("%+v\n", p)//Output struct type and field and value Main.point{x:1, y:2}Fmt. Printf ("% #v \ n", p)//Output type Main.pointFmt. Printf ("%t\n", p)//Output neat arrangement trueFmt. Printf ("%t\n",true)//Output Digital 123Fmt. Printf ("%d\n",123)//Output binaryFmt. Printf ("%b\n", -)//INT output to char typeFmt. Printf ("%c\n", -)//output 16 binary 1c8Fmt. Printf ("%x\n",456)//formatted output float type 78.900000Fmt. Printf ("%f\n",78.9)//1.234000e+08    //1.234000E+08Fmt. Printf ("%e\n",123400000.0) FMT. Printf ("%e\n",123400000.0)//Base string Output "string"Fmt. Printf ("%s\n","\" string\ "")//String output with reference base "\" string\ ""Fmt. Printf ("%q\n","\" string\ "")//Output byte 6865782074686973Fmt. Printf ("%x\n","Hex This")//Output address 0x42135100Fmt. Printf ("%p\n", &p)//Right-aligned output |   12| 345|Fmt. Printf ("|%6d|%6d|\n", A,345)//Right-aligned output |  1.20| 3.45|Fmt. Printf ("|%6.2f|%6.2f|\n",1.2,3.45)//Use-flag, left-aligned output |1.20 |3.45 |Fmt. Printf ("|%-6.2f|%-6.2f|\n",1.2,3.45)//Right-aligned output |   foo| b|Fmt. Printf ("|%6s|%6s|\n","foo","B")//Use-flag, left-aligned output |foo |b |Fmt. Printf ("|%-6s|%-6s|\n","foo","B")//Return only data not outputS: = Fmt. Sprintf ("a%s","string") FMT. PRINTLN (s)//Can format+print to IO. Writers other than OS. Stdout using fprintf.Fmt. fprintf (OS. Stderr,"an%s\n","Error")}

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.