Go Language FMT Package printf method detailed

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

The standard output stream of the go language differs from other languages (such as C # and Java) when printing to the screen, and here are some of the most common formatting input operations I've compiled.

General

    • %vPrint the value of a variable in the default way
    • %TTypes of print variables

Integer

    • %+dSigned integral type, fmt.Printf("%+d", 255) output+255
    • %qPrint single quotation marks
    • %oOctal with no 0
    • %#oOctal with 0
    • %xlowercase hexadecimal
    • %XHexadecimal in uppercase
    • %#xHex with 0x
    • %UPrint Unicode characters
    • %#UTo print Unicode with characters
    • %bBinary for printing integer type

Integer width

    • %5dIndicates that the maximum length of the integer is 5, and the following code

        fmt.Printf("|%5d|", 1)  fmt.Printf("|%5d|", 1234567)

The output results are as follows:

|     1| | 1234567|

    • %-5dInstead, the print results are automatically left-justified
    • %05d0 is prepended to the number.

Float

    • %f(=%.6f) 6-bit decimal point
    • %e(=%.6e) 6-bit decimal point (scientific notation)
    • %gUse the fewest numbers to represent
    • %.3gUp to 3 digits to indicate
    • %.3fUp to 3 decimal places to represent

String

    • %sNormal output string
    • %qstring with double quotation marks, quotes with escape characters in the string
    • %#qString with anti-quotation marks, if the string has anti-quotes, use double quotation marks instead of
    • %xConvert a string to lowercase 16 binary format
    • %XConvert a string to uppercase 16 binary format
    • % x16 binary format with spaces

String Width (example with 5)

    • %5sMinimum width of 5
    • %-5sMinimum width is 5 (left justified)
    • %.5sMaximum width of 5
    • %5.7sThe minimum width is 5 and the maximum width is 7
    • %-5.7sThe minimum width is 5 and the maximum width is 7 (left-justified)
    • %5.3sTruncated if the width is greater than 3
    • %05sIf the width is less than 5, the string is preceded by 0

Struct

    • %vNormal printing. Like what:{sam {12345 67890}}
    • %+vWith the field name. Like what:{name:sam phone:{mobile:12345 office:67890}
    • %#vUse go syntax to print.
      Like whatmain.People{name:”sam”, phone:main.Phone{mobile:”12345”, office:”67890”}}

Boolean

    • %tPrint true or False

Pointer

    • %pWith a 0x pointer
    • %#pWithout a 0x pointer

Reference

    • FMT. Printf format Reference (cheat sheet)
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.