Detailed explanation of Go string formatting example code
Go provides good support for string formatting. Let's look at some common string formatting examples.
package main import "fmt" import "os" type point struct { x, y int } func main() { // Go provides several print formats to format general Go values, for example // The following %v prints the value of an object in a point structure p := point{1, 2} ("%v\n", p) // If the formatted value is a structure object, then the formatted output of `%+v` // will include the member name and value of the structure ("%+v\n", p) // `%#v`Format output will output a Go syntax representation of a value. ("%#v\n", p) // Use `%T` to output a value's data type ("%T\n", p) // Format boolean variables ("%t\n", true) // There are many ways to format integers, using `%d` is one // Standard way of outputting integers in decimal ("%d\n", 123) // This way outputs the binary representation of integers ("%b\n", 14) // Print out the characters corresponding to the integer value here ("%c\n", 33) // Use `%x` to output a hexadecimal representation of a value ("%x\n", 456) // There are several formatting methods for floating-point values. The most basic one is `%f` ("%f\n", 78.9) // `%e` and `%E` use scientific notation to output integers ("%e\n", 123400000.0) ("%E\n", 123400000.0) // Use `%s` to output basic strings ("%s\n", "\"string\"") // To output a double quoted string like in Go source code, you need to use `%q` ("%q\n", "\"string\"") // `%x` outputs strings in hexadecimal, and each string's bytes are output with two characters ("%x\n", "hex this") // Use `%p` to output the value of a pointer ("%p\n", &p) // When outputting numbers, you often need to control the width and accuracy of the output. // You can use a number after % to control the width of the output, by default // In case the output is right-aligned with spaces on the left ("|%6d|%6d|\n", 12, 345) // You can also specify the output width of the floating point number, and you can also specify the floating point number // Output accuracy ("|%6.2f|%6.2f|\n", 1.2, 3.45) // To left-justify, use the `-` flag. ("|%-6.2f|%-6.2f|\n", 1.2, 3.45) // You can also specify the width of the output string to ensure that their output is aligned. default // In case, the output is right-aligned ("|%6s|%6s|\n", "foo", "b") // To use left alignment you can precede the width with the `-` sign ("|%-6s|%-6s|\n", "foo", "b") // The output of the `Printf` function is output to the command line`, you // You can use `Sprintf` to assign the formatted string to a variable s := ("a %s", "string") (s) // You can also use `Fprintf` to output the formatted value to ``` (, "an %s\n", "error") }
Running results
{1 2}
{x:1 y:2}
{x:1, y:2}
true
123
1110
!
1c8
78.900000
1.234000e+08
1.234000E+08
"string"
"\"string\""
6865782074686973
0xc000092000
| 12| 345|
| 1.20| 3.45|
|1.20 |3.45 |
| foo| b|
|foo |b |
a string
an error
Summarize
The above is a detailed explanation of the example code for Go string formatting introduced by the editor. I hope it will be helpful to everyone!
Related Articles
Golang RSA generation key, encryption, decryption, signature and signature verification implementation
RSA is the most commonly used asymmetric encryption algorithm. This article mainly introduces the implementation of Golang RSA generation key, encryption, decryption, signature and signature verification. It has certain reference value. If you are interested, you can learn about it.2023-11-11Detailed analysis of how to implement enumeration in Golang
Estimate means listing the data values one by one. Enumeration can be used to represent some fixed values. Enumeration is composed of constants. The following article mainly introduces relevant information about how to implement enumeration in Golang. Friends who need it can refer to it.2022-07-07How to implement string base64 encoding in go language
This article mainly introduces the method of implementing string base64 encoding in Go language. It analyzes the skills of operating strings in Go language and the skills of using base64 encoding. Friends who need it can refer to it.2015-03-03Detailed explanation of the next larger element example of Go language problem LeetCode
This article mainly introduces a detailed explanation of the next larger element example of Go language problem solving LeetCode. Friends in need can refer to it for reference. I hope it will be helpful. I wish you more progress and get promoted as soon as possible to get a salary increase.2022-12-12Go language--Slice details
This article mainly introduces Go language - Slice (Slice). Go language slice is an abstraction of arrays. The following article editor will introduce this content in detail to you. Friends who need it can refer to it. I hope it will be helpful to you.2021-10-10Detailed explanation of the graphics and text of go language import error reporting
Today I originally wanted to try public and private methods in go language, but when I import other packages, I directly reported an error. The following article mainly introduces relevant information about the processing of go language import errors. Friends who need it can refer to it.2023-04-04Basic knowledge of GO literacy precautions
This article mainly introduces the basic knowledge and precautions of GO. This article is a lieberal text for GO language novels. It mainly explains the basic knowledge of GO language, GO program directory structure, import and alias of GO packages, built-in keywords, GO annotation methods, friends who need to use GO comments can refer to it.2022-12-12Usage of the Go type conversion toolkit strconv package
The strconv package of Go language provides functions for conversion between basic data types. This article mainly introduces the usage of the strconv package of the Go type conversion toolkit, which has certain reference value. If you are interested, you can learn about it.2024-05-05How to determine whether a file or folder exists in Go language
This article mainly introduces the method of judging whether a file or folder exists in Go. It compares and analyzes Go's operation skills and related precautions for judging files and directories in Go. Friends who need it can refer to it.2017-05-05Go's HTTP interface speed limit practice based on Gin framework
HTTP interface plays an important role among various business modules. This article mainly introduces the speed limit practice of go's HTTP interface based on Gin framework. It has certain reference value. If you are interested, you can learn about it.2023-09-09