package main import "fmt" type Person struct { Name string Age int } func main() { person := Person{ Name: "test", Age: 15, } ("%v\n", person) ("%+v\n", person) ("%#v\n", person) }
The execution output is as follows:
{test 15}</code><code>{Name:test Age:15}</code><code>{Name:"test", Age:15}
1 Placeholder %v, %+v, %#v description
Placeholder Description
%v Output all value information
%+v The field name will be printed when printing the structure
%#v Go syntax representation of the corresponding value
2 Other placeholder descriptions
2.1 Boole
Placeholder illustrate Give an example Output %t true or false。Printf("%t", true) true
2.2 Integer
Placeholder illustrate Give an example Output %b Binary representation Printf("%b", 5) 101 %c correspondingUnicodeCharacters represented by code points Printf("%c", 0x4E2D) middle %d Decimal representation Printf("%d", 0x12) 18 %o Octal representation Printf("%d", 10) 12 %q Character literal value surrounded by single quotes,Depend onGoSyntax escapes safely Printf("%q", 0x4E2D) 'middle' %x Hexadecimal representation,Letter form is lowercase a-f Printf("%x", 13) d %X Hexadecimal representation,Letter form in capital A-F Printf("%x", 13) D %U UnicodeFormat:U+1234,Equivalent to "U+%04X" Printf("%U", 0x4E2D) U+4E2D
2.3 String and byte slices
Placeholder illustrate Give an example Output %s Output字符串表示(stringType or[]byte) Printf("%s", []byte("Go")) Golanguage %q String surrounded by double quotes,Depend onGoSyntax escapes safely Printf("%q", "Go") "Go" %x hexadecimal,Lowercase letters,Two characters per byte Printf("%x", "golang") 676f6c616e67 %X hexadecimal,uppercase letter,Two characters per byte Printf("%X", "golang") 676F6C616E67
2.4 Pointer
Placeholder illustrate Give an example Output %p Hexadecimal representation,Prefix 0x Printf("%p", &people) 0x4f57f0
2.5 Floating point numbers and complex numbers
Placeholder illustrate Give an example Output %b No decimal parts,Scientific notation method of power of exponential two, and of 'b' Consistent conversion format。For example -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 exponent,For example 123.456 Printf("%f", 10.2) 10.200000 %g Choose according to the situation %e or %f 以产生更紧凑of(无末尾of0)Output Printf("%g", 10.20) 10.2 %G Choose according to the situation %E or %f 以产生更紧凑of(无末尾of0)Output Printf("%G", 10.20+2i)
Attachment: The difference between %v and %+v when printing the Go language structure
In Go,%v
and%+v
All are placeholders for formatting the printing structure. Their differences are as follows:
- %v: The default format is used to print the structure, only all field values of the structure are output, and the fields are separated by spaces.
- %+v: Format the printing structure, the structure type and field name will be output, and the fields are separated by spaces.
For example, suppose there is a structurePerson
The definition is as follows:
Go
type Person struct { Name string Age int }
If aPerson
Type variablep
, and use()
When the function prints it, the output result is as follows:
Go
p := Person{Name: "John Doe", Age: 30} (p) // {John Doe 30}
use%v
The placeholder prints the structure and only outputs all field values of the structure, i.e.John Doe
and30
。
If used%+v
Placeholder prints the structure, and the structure type and field name will be output, i.e.Person{Name: John Doe, Age: 30}
。
Summarize:
-
%v
: The default format is the printing structure and only the field values are output. -
%+v
: Format the printing structure, output the structure type and field name.
Select the appropriate placeholder as needed to print the structure.
Summarize
This is the introduction to this article about the detailed explanation of golang placeholders %v, %+v, %#v. For more related go placeholders %v, %+v, %#v, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!