1. `Sprintf` formats and returns a string without any output.
s := ("is string %s","string")
2. (s) // is a string %s corresponds to a string string
3. You can use `Fprintf` to format and output
(, "Format %s\n", "error")
Let's talk about the formatted symbols:
/* %v output structure {10 30} %+v The field name of the output structure display {one:10 tow:30} %#v Output structure source code snippet {one:10, tow:30} %T The type of output value %t Output formatted boolean true %d` output standard decimal format 100 %b` output standard binary format 99 corresponding to 1100011 %c` outputs the corresponding character of a fixed integer 99 corresponding c %x` output hexadecimal code 99 corresponding to 63 %f` output decimal format 99 corresponding to 63 %e`Output scientific scientific notation representation form 123400000.0 corresponding to 1.234000e+08 %E`Output scientific scientific notation representation form 123400000.0 corresponding to 1.234000e+08 %s performs basic string output "\"string\"" corresponding to "string" %q The output with double quotes in the source code "\"string\"" corresponds to "\"string\"" %p outputs the value of a pointer &jgt corresponds to 0xc00004a090 % Use numbers to control the output width. The default result is right-aligned and fills the blank part with spaces. %2.2f Specifies the output width of the floating point type 1.2 corresponding to 1.20 %*2.2f Specifies the output width alignment of the floating point type, using the `-` flag 1.2 corresponding to *1.20 */ jgt := Point{10,30} ("%v\n",jgt) // {10 30} ("%+v\n",jgt) // {one:10 tow:30} ("%#v\n",jgt) // {one:10, tow:30} ("%T\n",jgt) // ("%t\n",true) // true ("%d\n",100) // 100 ("%b\n",99) // 99 corresponding to 1100011 ("%c\n",99) // 99 corresponding c ("%x\n",99) // 99 Correspondence 63 ("%f\n",99.9) // 99.9 corresponds to 99.900000 ("%e\n",123400000.0) // 123400000.0 corresponds to 1.234000e+08 ("%E\n",123400000.0) // 123400000.0 corresponds to 1.234000e+08 ("%s\n","\"string\"") // "\"string\"" corresponding to "string" ("%q\n","\"string\"") // "\"string\"" corresponding to "string" ("%p\n",&jgt) // &jgt corresponds to 0xc00004a090 ("%6d\n",8) // &jgt corresponds to 0xc00004a090 ("%2.2f\n",1.2 ) // 1.2 corresponds to 1.20 ("*%2.2f\n",1.2 ) // 1.2 corresponds to *1.20
Supplement: Go Study Notes: The difference between Println and Printf, and the detailed usage of Printf
Println and Printf are both public methods in the fmt package. These two functions need to be used when printing information. So what is the difference between these two functions?
Println
: Can print out strings and variables
Printf
: Only formatted strings can be printed, string type variables can be output, and shaping variables and shaping can not be output.
In other words, when you need to format the output information, Printf is generally selected, and Println is also available at other times, such as:
a := 10 (a)//right ("abc")//right ("%d",a)//right (a)//error
Printf Detailed usage
When using Printf, you need to specify a formatting rule, that is, the first parameter. So how many formats are there in total, and how do you need to write it when using it?
package main import "fmt" import "os" type point struct { x, y int } func main() { //Go provides a variety of printing methods for formatting design of conventional Go values. For example, an instance of the point structure is printed here. p := point{1, 2} ("%v\n", p) // {1 2} //If the value is a structure, the formatted output content of %+v will include the field name of the structure. ("%+v\n", p) // {x:1 y:2} //%The #v form outputs the Go syntax representation of this value. For example, a value runs the source code snippet. ("%#v\n", p) // {x:1, y:2} //The type of value that needs to be printed is used, use %T. ("%T\n", p) // //Formatting boolean values is simple. ("%t\n", true) //There are many ways to format the shaping number, using %d for standard decimal formatting. ("%d\n", 123) //This output binary representation. ("%b\n", 14) //This outputs the corresponding character of the given integer. ("%c\n", 33) //%x provides hexadecimal encoding. ("%x\n", 456) //There are also many formatting options for floating point types. Use %f for the most basic decimal formatting. ("%f\n", 78.9) //%e and %E format the floating point type into (slightly different) scientific scientific notation representation. ("%e\n", 123400000.0) ("%E\n", 123400000.0) //Use %s for basic string output. ("%s\n", "\"string\"") //Output with double quotes like in Go source code, use %q. ("%q\n", "\"string\"") //Same as the shaping number above, the %x output uses a base-16-encoded string, and each byte is represented by 2 characters. ("%x\n", "hex this") //To output a pointer value, use %p. ("%p\n", &p) //When outputting numbers, you will often want to control the width and accuracy of the output result. You can use numbers after % to control the output width. The default result is right-aligned and fills the blank section with spaces. ("|%6d|%6d|\n", 12, 345) //You can also specify the output width of the floating point type, and you can also specify the output accuracy through the width.precision syntax. ("|%6.2f|%6.2f|\n", 1.2, 3.45) //To be most aligned, use the - flag. ("|%-6.2f|%-6.2f|\n", 1.2, 3.45) //You may also want to control the width of the string output, especially to ensure they are aligned when outputting the class table. This is a basic right-aligned width representation. ("|%6s|%6s|\n", "foo", "b") //To be left aligned, just like numbers, use the - flag. ("|%-6s|%-6s|\n", "foo", "b") // So far, we've seen Printf, which outputs formatted strings. Sprintf formats and returns a string without any output. s := ("a %s", "string") (s) //You can use Fprintf to format and output to instead. (, "an %s\n", "error") }
You can see that there are quite a lot of these types, but the first ones are often used, or it can be said that the most used one is the first one.
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.