Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the placeholders in the Go language

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "what are the placeholders in Go language". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the placeholders in the Go language.

I. concept

What is a placeholder? You understand from the surface, it is to occupy a place, but there are many different kinds of positions, and this position is not everyone can sit, it is required, it is used to format data at the program level.

2. Which functions support

In the Go language, the standard package fmt has many formatted utility functions, and 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.

Fmt.Sscanf parses the corresponding position data of the placeholder from the string.

Fmt.Fscanf reads data from the io.Reader type and parses the corresponding position data of placeholders for reading files, terminals (standard input).

Fmt.Fprintf outputs formatted string data to an io.Writer type for export to a file.

Fmt.Errorf formatting creates an error type message.

This piece does not focus on the expansion of each function, just a simple list, if you do not understand to check it yourself, you can also ask me.

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

III. Use of placeholders

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

S: = fmt.Sprintf ("% s really handsome", "Lao Miao") fmt.Println (s) / / output Lao Miao really handsome

% s marks the string and populates the "Lao Miao" string to the% s position. Come down and see what placeholders there are.

IV. General placeholder

First create a data as a printed object.

Type Example struct {Content string} var data = Example {Content: "examples"} 1.% v,% + v,% # v

% v: gets the value of the data and, if the error interface is implemented, only represents an error message.

Fmt.Printf ("% v", data) / / output {example} fmt.Printf ("% v", errors.New ("I was wrong") / / output I was wrong

% + v: gets the value of the data and, if the structure, carries the field name.

Fmt.Printf ("% + v", data) / / output {Content: example}

% # v: gets the value of the data and, if it is a structure, carries the structure name and field name.

Fmt.Printf ("% # v", data) / / output main.Example {Content: "example"} 2.% T

Get data type

Fmt.Printf ("% T", data) / / output main.Example3. %%

Literally a percent sign.

Fmt.Printf ("%") / / output% V, Boolean placeholder

% t

True or false.

Fmt.Printf ("% t", true) / / output true six, integer placeholder 1.% b

Binary.

Fmt.Printf ("% b", 4) / / output 1002. % c

Unicode code is converted to characters.

Fmt.Printf ("% c", 0x82d7) / / output seedlings 3.% d,% 5d,%-5d, d

Decimal integer representation.

Fmt.Printf ("% DJ% dJ% d", 10,010, 0x10) / / output 10J 8J 16

Three data: 10 decimal, 010 octal, 0x10 hexadecimal

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

% 5d%-5dd

Example:

Fmt.Printf ("|% 5d |%-5d | d |", 1,1,1) / / output | 1 | 1 | 00001 | 4.% o,% # o

Octal representation

Fmt.Printf ("% OMagol% OMA% o", 10010, 0x10) / / output 12meme10 Eng 20

In many development languages, the number that starts with 0 represents octal. The% # o output starts with 0.

Fmt.Printf ("\ n% roomo\ n", 10) / / output 0125. % Q

Similar to% c, all Unicode codes are converted to characters, but the result is extra single quotation marks.

Fmt.Printf ("% Q", 0x82d7) / / output 'vaccine'

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

6.% x,% # x

Hexadecimal representation, the letter form is lowercase amurf, and the% # x output starts with 0x.

Fmt.Printf ("% x,% # x", 13,13) / / output d, 0xd7. % X,% # X

Hexadecimal representation, the letter form is lowercase Amurf, and the% # X output begins with 0X.

Fmt.Printf ("% X,% # X", 13,13) / / output D, 0XD8. % U,% # U

% U: converted to Unicode format specification.

Fmt.Printf ("% U", 0x82d7) / / output U+82D7

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

Fmt.Printf ("% # U", 0x82d7) / / output U+82D7 'Miao' 7, floating point and plural 1.% b

The scientific counting method of converting a floating point number to the power of 2.

Fmt.Printf ("% b", 0.1) / / output 7205759403792794p-562. % e,% E

The scientific counting of the power of 10.

Fmt.Printf ("% e", 10.2) / / output 1.020000e+01fmt.Printf ("% E", 10.2) / / output 1.020000E+01 difference: case difference between% e and% E output. 3.% f,% .2f

Floating point,% .2f means to retain 2 decimal places,% f retains 6 places by default, and% f is equivalent to% F.

I have not yet figured out the rules of reservation. Sometimes they conform to rounding and sometimes they do not. Let me come down and study them and tell members.

Fmt.Printf ("% f", 10.2) / / output 10.200000fmt.Printf ("% .2f |% .2f", 10.232, 10.235) / / output 10.23 | 10.23

You can also add a minimum width, as follows:

% 9.2f has a minimum width of 9, including decimal places, and a precision of 2.

The minimum width of% 9.f or% 9f is 9.

4.% g,% .3G

Select% e or% f as appropriate, but remove 0 at 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 is as follows:

Fmt.Printf ("% g |% g", 2e2, 2E2) / / output 200 | 200 |

%. 3G means that instead of keeping three decimal places, it only retains three digits.

Fmt.Printf (".3G", 12.34) / / output 12.3

Thinking: there is a difference between% g and% G in the official website, but it is equivalent when I test it. Maybe there is something wrong with my test. I give the documents on the official website as follows:

% g% e for large exponents,% f otherwise. Precision is discussed below.%G E for large exponents, F otherwise 8, string and byte slices 1.% s

String or byte slice

Fmt.Printf ("% s |% s", "Lao Miao", [] byte ("hey hey")) / / output Lao Miao | hey hey 2.% Q

There are Go language security escape, double quotation mark package.

Fmt.Printf ("% Q", "Lao Miao") / / output "Lao Miao" 3.% x,% X

Hexadecimal,% x lowercase letters a-f,% X capital letters A-F.

Fmt.Printf ("% x |% X", "Miao", "Miao") / / output e88b97 | E88B979, pointer

% p,% # p

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

Num: = 2s: = [] int {1,2} fmt.Printf ("% p |% p", & num, s) / / output 0xc00000a1d0 | 0xc00000a1e0 + other tags 1. +

Print the plus or minus sign of the numeric value. For% + Q, only ASCII-encoded characters are output, and if non-ASCII-encoded, it is converted to Unicode-encoded output.

Fmt.Printf ("% + d |% + d", 2,-2) / output + 2 |-2fmt.Printf ("% + Q |% + Q", "miao", "Miao") / / output "miao" | "\ u82d7" 2.-

Fill in the blanks on the right, this piece is not an example, use such as%-5d, floating point%-9.2f is also supported, other placeholders you can be interested in their own experiment.

3.

Add a leading 0 to the octal, as shown above.

Add a leading 0x or 0X to hexadecimal, as shown above.

Remove 0x for% # p

% + Q uses backquotation marks when printing strings.

Fmt.Printf ("% # Q", "Miao") / / output `Miaon`

% # U prints the code with characters, as shown above.

4. '' Spac

Leave blank space for the plus or minus sign.

Fmt.Printf ("|% d |", 2) / / output | 2 | 5.0

Fill in the leading 0, and non-digits can also be used if the number is moved after the plus or minus sign.

Fmt.Printf ("s", "a") / / output 0000afmt.Printf ("% + 05d", 1) / / output + 0001

Precision truncation string

Use precision for strings to truncate strings.

Fmt.Printf (".2s", "abc") / / output ab

Summary

Placeholders are case-sensitive, a total of 20, but there are actually other placeholder-related knowledge points, I do not want to study for the time being, because it is also difficult to use in the project.

At this point, I believe you have a deeper understanding of "what are the placeholders in the Go language?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report