In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to format Go language strings". In daily operation, I believe many people have doubts about how to format GE language strings. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to format Go language strings". Next, please follow the editor to study!
Go provides good support for string formatting. Let's look at some examples of commonly used string formatting.
Package mainimport "fmt" import "os" type point struct {x, y int} func main () {/ / Go provides several print formats for formatting general go values, such as / / the following% v prints the value of an object with a point structure p: = point {1,2} fmt.Printf ("% v\ n", p) / / if the formatted value is a structure object Then the formatted output of `% + v` will include the member name of the structure and the value fmt.Printf ("% + v\ n", p) / /`% # v` formatted output will output the Go syntax representation of a value. Fmt.Printf ("% # v\ n", p) / / use `% T` to output the data type fmt.Printf ("% T\ n", p) / / format the Boolean variable fmt.Printf ("% t\ n", true) / / there are many ways to format an integer Using `% d` is a / / standard way to output an integer in decimal fmt.Printf ("% d\ n", 123) / / the binary representation of the integer fmt.Printf ("% b\ n", 14) / / the character fmt.Printf ("% c\ n") corresponding to the integer value is printed here. 33) / / using `% x` to output a hexadecimal representation of a value fmt.Printf ("% x\ n", 456) / / floating-point values are also formatted in several ways. The most basic one is `% f` fmt.Printf ("% f\ n", 78.9) /`% e` and `% E` to output integer fmt.Printf ("% e\ n", 123400000.0) fmt.Printf ("% E\ n", 123400000.0) / / use`% s` to output the basic string fmt.Printf ("% s\ n"). "\" string\ ") / / outputs a string with double quotes as in the Go source code You need to use `% q` fmt.Printf ("% Q\ n", "\" string\ ") / /`% x` to output the string in hexadecimal, and each byte of the string output fmt.Printf ("% x\ n "," hex this ") / / use `% p` to output the value of a pointer fmt.Printf ("% p\ n ", & p) / / when you output a number It is often necessary to control the width and precision of the output. / / you can use a number after% to control the width of the output. By default, the output is aligned to the right, with a space fmt.Printf ("|% 6d |% 6d |\ n", 12345) / / you can also specify the output width of the floating point number. You can also specify the output precision of floating point number / / fmt.Printf ("|% 6.2f |% 6.2f |\ n", 1.2,3.45) / / To left-justify, use the `-` flag. Fmt.Printf ("|%-6.2f |%-6.2f |\ n", 1.2,3.45) / / you can also specify the width of the output strings to ensure their output alignment. By default, the output is right-aligned fmt.Printf ("|% 6s |% 6s |\ n", "foo", "b") / / in order to use left alignment, you can add the `-` sign fmt.Printf ("|%-6s |%-6s |\ n", "foo", "b") / / the output of the function is output to the command line `os.Stdout`. You can use `Sprintf` to assign the formatted string to a variable s: = fmt.Sprintf ("a% s", "string") fmt.Println (s) / / you can also use `Fprintf` to output the formatted value to `io.Writers` fmt.Fprintf (os.Stderr, "an% s\ n", "error")}
Running result
Main.pointtrue1231110main.pointtrue12311101c878.9001.234000Eyog08 "string"\ "string\" 68657820746869730xc000092000 | 12 | 345 | 1.45 | 1.45 | foo | b | foo | b | a stringan error so far, the study on "how to format Go language strings" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.