Learn Go: 17. Explain 20 placeholders in detail

Posted by ccgorman on Tue, 26 Oct 2021 08:00:26 +0200

>>Original address

Learn what

  1. What is a placeholder?

  2. Which functions are supported?

  3. How do I use placeholders?

  4. The role of different placeholders?

  5. How many marker symbols are used with placeholders?

concept

What is a placeholder? You can understand from the surface meaning that it is to occupy a position, but there are many different kinds of positions, and this position can not be sat by anyone. It is required to format data at the program level.

Which functions are supported

In Go language, the standard package fmt has many formatted tool functions. The function name usually ends with f, as listed below:

  • fmt.Printf formats the string and prints it to the terminal (standard output).

  • fmt.Sprintf formats the string and returns it.

  • Fmt.scanf parses the corresponding position data of the placeholder from the string.

  • fmt.Fscanf reads data from io.Reader type and parses the corresponding position data of placeholder for reading files and terminals (standard input).

  • fmt.Fprintf outputs the formatted string data to the io.Writer type for output to a file.

  • Fmt.errorformat creates an error type message.

This section does not focus on explaining various functions, but simply lists them. If you don't understand them, you can also ask me.

Let's take a look at the key points of this article

Placeholder use

Placeholders need to be represented by the% symbol. Simply show one.

s := fmt.Sprintf("%s How handsome", "Old seedling")

fmt.Println(s)

// output

Lao Miao is so handsome

%S indicates the string and fills the "old seedling" string to the position% s. Come down and see what placeholders are there?

Normal placeholder

First create a data as the printed object.

type  Example  struct {

Content string

}

var  data = Example{Content: "example"}

1. %v,%+v,%#v

%v: Gets the value of the data. If the error interface is implemented, it only represents an error message.

fmt.Printf("%v", data)

// output

{example}

fmt.Printf("%v", errors.New("I was wrong"))

// output

I was wrong

%+v: Get the value of the data. If it is a structure, it will carry the field name.

fmt.Printf("%+v", data)

// output

{Content:example}

%#v: Get the value of the data. If it is a structure, it will carry the structure name and field name.

fmt.Printf("%#v", data)

// output

main.Example{Content:"example"}

2. %T

Gets the data type.

fmt.Printf("%T", data)

// output

main.Example

3. %%

A literal percent sign.

fmt.Printf("%%")

// output

%

Boolean placeholder

%t

true or false.

fmt.Printf("%t", true)

// output

true

Integer placeholder

1. %b

Binary.

fmt.Printf("%b", 4)

// output

100

2. %c

Unicode to character.

fmt.Printf("%c", 0x82d7)

// output

seedling

3. %d,%5d,%-5d,%05d

Decimal integer representation.

fmt.Printf("%d,%d,%d", 10, 010, 0x10)

// output

10,8,16
  • Three data: 10 decimal, 010 octal, 0x10 hexadecimal

Placeholders can also specify a minimum width in the following format:

  • %5d: the minimum width is 5, aligned right and left blank.

  • %-5d: align left and leave blank on the right.

  • %05d: when the number of digits is less than 5, fill zero on the left.

Example:

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

// output

|    1|1 |00001|

4. %o,%#o

Octal representation.

fmt.Printf("%o,%o,%o", 10, 010, 0x10)

// output

12,10,20

In many development languages, numbers beginning with 0 represent octal. Start with 0 through%#o output.

fmt.Printf("\n%#o\n", 10)

// output

012

5. %q

Similar to% c, they are Unicode to Unicode characters, but the result has more single quotation marks.

fmt.Printf("%q", 0x82d7)

// output

'seedling'

Chinese character correspondence table: www.chi2ko.com/tool/CJK.htm

6. %x,%#x

Hexadecimal representation, the letter form is lowercase a-f,%#x output starts with 0x.

fmt.Printf("%x, %#x", 13, 13)

// output

d, 0xd

7. %X,%#X

Hexadecimal representation, the letter form is lowercase A-F,%#X output starts with 0X.

fmt.Printf("%X, %#X", 13, 13)

// output

D, 0XD

8. %U,%#U

%U: Convert to Unicode format specification.

fmt.Printf("%U", 0x82d7)

// output

U+82D7

%#U: Convert to Unicode format with corresponding characters.

fmt.Printf("%#U", 0x82d7)

// output

U+82D7  'seedling'

Floating point number and complex number

1. %b

Scientific counting method for converting floating-point numbers to powers of 2.

fmt.Printf("%b", 0.1)

// output

7205759403792794p-56

2. %e,%E

Scientific counting method of the power of 10.

fmt.Printf("%e", 10.2)

// output

1.020000e+01

fmt.Printf("%E", 10.2)

// output

1.020000E+01

Difference:% E and% E output case.

3.% F,%. 2f, etc

Floating point number,%. 2f means to reserve 2 decimal places,% f reserves 6 decimal places by default,% F is equivalent to% F.

I haven't figured out the reserved rules yet. Sometimes it conforms to rounding and sometimes it doesn't. let me study it and tell you later.

fmt.Printf("%f", 10.2)

// output

10.200000

fmt.Printf("%.2f|%.2f", 10.232, 10.235)

// output

10.23|10.23

The minimum width can also be added as follows:

  • %9.2f the minimum width is 9, including decimal places, and the accuracy is 2.

  • %9.f or%9f width 9 min.

4. %g,%.3g

Select% e or% f as appropriate, but remove 0 from the end.

%f the situation is as follows:

fmt.Printf("%g|%g", 10.20, 1.200000+3.400000i)

// output

10.2|(1.2+3.4i)

%e the situation is as follows:

fmt.Printf("%g|%g", 2e2, 2E2)

// output

200|200

%. 3g does not mean that 3 decimal places are reserved, but only 3 numbers are reserved.

fmt.Printf("%.3g", 12.34)

// output

12.3

Thinking: there are differences between% G and% G on the official website, but they are equivalent after my test. There may be problems with my test. I give the official website documents as follows:

%g  %e for large exponents, %f otherwise. Precision is discussed below.

%G  %E for large exponents, %F otherwise

String and byte slicing

1. %s

String or byte slice.

fmt.Printf("%s|%s", "Old seedling", []byte("Hey, hey, hey"))

// output

Old seedling|Hey, hey, hey

2. %q

Safe escape in Go language, wrapped in double quotation marks.

fmt.Printf("%q", "Old seedling")

// output

"Old seedling"

3. %x,%X

Hexadecimal,% x lowercase letters a - F,% x uppercase letters a - F.

fmt.Printf("%x|%X", "seedling", "seedling")

// output

e88b97|E88B97

Pointer

%p,%#p

Address, expressed in hexadecimal,% P with 0x,%#p without.

num := 2

s := []int{1, 2}

fmt.Printf("%p|%p", &num, s)

// output

0xc00000a1d0|0xc00000a1e0

Other marks

1. +

Print the sign of the value. For% + q, only ASCII coded characters are output. If it is not ASCII coded, it is converted to Unicode coded output.

fmt.Printf("%+d|%+d", 2, -2)

// output

+2|-2

fmt.Printf("%+q|%+q", "miao","seedling")

// output

"miao"|"\u82d7"

2. -

Fill in the space on the right. This is not an example. For example,% - 5d and floating point% -9.2f are also supported. You can experiment with other placeholders by yourself.

3.

  • Add leading 0 for octal, as shown in the example above.

  • Add leading 0X or 0X for hexadecimal, as illustrated above.

  • Remove 0x for%#p.

  • %+q use backquotes when printing strings.

fmt.Printf("%#q "," Miao ")

// output

`seedling`
  • %#U print code with characters, as shown in the above example.

4. Space

Leave space for the sign.

fmt.Printf("|% d|", 2)

// output

| 2|

5. 0

Fill in the leading 0. For numbers, they will be moved after the sign. Non numbers can also be used.

fmt.Printf("%05s", "a")

// output

0000a

fmt.Printf("%+05d", 1)

// output

+0001

Precision truncated string

Use precision for the string to truncate the string.

fmt.Printf("%.2s", "abc")

// output

ab

summary

Placeholders are case sensitive. I have talked about 20 in total, but there are still some knowledge points related to placeholders. I don't want to study them for the time being, because they are also difficult to use in the project.

If you are interested, go to the official website.

Address: pkg.go.dev/fmt

Topics: Go