This is a creation in Article, where the information may have evolved or changed.
The following excerpt from the Go programming Language:
When printing numbers using the FMT package, we can control the radix and format with the%d,%o, and%x verbs, as shown I N This example:
o: = 0666
Fmt. Printf ("%d%[1]o%#[1]o\n", O)//"438 666 0666"
x: = Int64 (0xdeadbeef)
Fmt. Printf ("%d%[1]x%#[1]x%#[1]x\n", x)
Output:
3735928559 Deadbeef 0xdeadbeef 0XDEADBEEF
Note the use of both FMT tricks. Usually a Printf format string containing multiple% verbs would require the same number of extra operands, but the [1] "a Dverbs "After%-tell Printf-to-use the first operand over and over again. Second, the # adverb for%o or%x or%x tells Printf to emit a 0 or 0x or 0X prefix respectively.
Rune literals is written as a character within single quotes. The simplest example is an ASCII character like ' a ', but it's possible to write any Unicode code point either directly or With numeric escapes, as we'll see shortly.
Runes is printed with%c, or with%q if quoting is desired:ascii: = ' a '
Unicode: = ' '
NewLine: = ' \ n '
Fmt. Printf ("%d%[1]c%[1]q\n", ASCII)//"$ A ' a '"
Fmt. Printf ("%d%[1]c%[1]q\n", Unicode)//"22269" "
Fmt. Printf ("%d%[1]q\n", newline)//"Ten ' \ n '"
Sum up, " %[1]
" or format the first argument; "" %#
prints the prefix of the numeric value, and " %q
" adds quotation marks. Examples are as follows:
package mainimport "fmt"func main() { var c rune = '楠' fmt.Printf("%c %[1]d %#[1]x %[1]q", c)}
Execution Result:
楠 26976 0x6960 '楠'