Golang FMT format "placeholder"

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

This article is forwarded, the original address

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

# 定义示例类型和变量type Human struct {    Name string}var people = Human{Name:"zhangsan"}

Common placeholder

placeholder Description Example Output
%v The default format for the corresponding value. Printf ("%v", people) {Zhangsan}
%+v Field names are added when the structure is printed Printf ("%+v", people) {Name:zhangsan}
% #v The go syntax representation of the corresponding value Printf ("#v", people) Main. Human{name: "Zhangsan"}
%T The go syntax for the type of the corresponding value Printf ("%T", people) Main. Human
%% Literal percent-semicolon, not a placeholder for a value PRINTF ("percent") %

Boolean placeholder

placeholder Description Example Output
%t True or FALSE. Printf ("%t", True) True

Integer placeholders

placeholder Description Example Output
%b Binary representation Printf ("%b", 5) 101
%c The character represented by the corresponding Unicode code point Printf ("%c", 0x4e2d) In
%d Decimal representation Printf ("%d", 0x12) 18
%o Octal representation Printf ("%d", 10) 12
%q Character literals surrounded by single quotes, safely escaped by go syntax Printf ("%q", 0x4e2d) In
%x Hexadecimal representation, alphabetic form lowercase a-f Printf ("%x", 13) D
%x Hexadecimal representation, uppercase a-f in alphabetic form Printf ("%x", 13) D
%u Unicode format: u+1234, equivalent to "u+%04x" Printf ("%u", 0x4e2d) U+4e2d

Components of floating-point numbers and complex numbers (real and imaginary parts)

placeholder Description Example Output
%b The scientific notation of a power with no fractional part, exponential two, and StrConv. The formatfloat ' B ' conversion format is consistent. such as -123456p-78
%e Scientific counting method, for example -1234.456e+78 Printf ("%e", 10.2) 1.020000e+01
%E Scientific counting method, for example -1234.456E+78 Printf ("%e", 10.2) 1.020000E+01
%f Have a decimal point but no index, for example 123.456 Printf ("%f", 10.2) 10.200000
%g Select%e or%f to produce a more compact (no end 0) output, depending on the situation Printf ("%g", 10.20) 10.2
%G Select%E or%f to produce a more compact (no end 0) output, depending on the situation Printf ("%G", 10.20+2i) (10.2+2i)

String and byte slices

placeholder Description Example Output
%s Output string representation (string type or []byte) Printf ("%s", []byte ("Go Language")) Go language
%q String surrounded by double quotes, safely escaped by go syntax Printf ("%q", "Go Language") "Go Language"
%x Hex, lowercase 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
%p Hexadecimal representation, prefix 0x Printf ("%p", &people) 0x4f57f0

Other marks

placeholder Description Example Output
+ The positive and negative number of the total printed value; for%q (%+ Q) Ensure that only ASCII-encoded characters are output. printf ("%+q", "Chinese") "\u4e2d\u6587"
-
# alternate format: Add a leading 0 (% #o) for octal printf ("% #U", ' Medium ') u+4e2d
"
0 padding leading 0 instead of spaces For numbers, this moves the fill to the sign after the
    • Golang does not have a '%u ' dot, and if the integer is an unsigned type, it is printed as unsigned by default. The control format for

    • Width and precision is in Unicode code points. 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 available as characters ' * '.

    • For%g/%g, the precision is the total number of all numbers, for example: 123.45,%.4g prints 123.5 (and%6.2f prints 123.45). The default precision for

    • %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.

    • With a string type, with precision as the maximum number of characters to output, and 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.