Go input and output formatting

Source: Internet
Author: User
Tags sprintf
Type example struct {
    a int
    b string
}

Prints an instance of the example struct.
P: = Example{1, 2}
Fmt. Printf ("%v\n", p)

If the value is a struct, the formatted output of%+V will include the field name of the struct body.
Fmt. Printf ("%+v\n", p)

The% #v form is the output of the GO syntax for that value. For example, the value of the run source snippet.
Fmt. Printf ("% #v \ n", p)

The type of value to be printed, using%T.
Fmt. Printf ("%t\n", p)

Formatting a Boolean value is simple.
Fmt. Printf ("%t\n", True)

There are several ways to format the shaping number, using%d for the standard decimal

System format.
Fmt. Printf ("%d\n", 123)

This binary representation of the output.
Fmt. Printf ("%b\n", 14)

This output corresponds to the character of the given integer.
Fmt. Printf ("%c\n", 33)

%x provides hexadecimal encoding.
Fmt. Printf ("%x\n", 456)

There are also many formatting options for floating-point types. Use%f to enter

The most basic decimal format for the line.
Fmt. Printf ("%f\n", 78.9)

%e and%e The floating-point format (a little bit less

The same) scientific notation of notation.
Fmt. Printf ("%e\n", 123400000.0)
Fmt. Printf ("%e\n", 123400000.0)

Use%s for basic string output.
Fmt. Printf ("%s\n", "\" string\ "")

Like the output in the Go source code with double quotes, use%q.
Fmt. Printf ("%q\n", "\" string\ "")

As with the number above, the%x output uses the BASE-16 encoded word

Each byte is represented by a 2-character string.
Fmt. Printf ("%x\n", "Hex This")

To output a pointer value, use%p.
Fmt. Printf ("%p\n", &p)

When outputting a number, you will often want to control the width and precision of the output, and you can use the number after% to control the output width. The default results use right-aligned and fill the blanks with spaces.
Fmt. Printf ("|%6d|%6d|\n", 12, 345)

Specifies the output width of a floating-point type, and the precision of the output can be specified by the syntax of width and precision.
Fmt. Printf ("|%6.2f|%6.2f|\n", 1.2, 3.45)

To be most aligned, use the-flag.
Fmt. Printf ("|%-6.2f|%-6.2f|\n", 1.2, 3.45)

Controls the width of the string output, ensuring that they are aligned when the class table is output. This is the basic right-aligned width representation.
Fmt. Printf ("|%6s|%6s|\n", "foo", "B")

To align Left, as with numbers, use the-flag.
Fmt. Printf ("|%-6s|%-6s|\n", "foo", "B")

SPRINTF formats and returns a string without any output.
S: = Fmt. Sprintf ("A%s", "string")
Fmt. PRINTLN (s)

You can use fprintf to format and output to IO. Writers rather than the OS. Stdout.
Fmt. fprintf (OS. Stderr, "An%s\n", "error")

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.