[Golang] Golang formatted output

fmt

The standard library commonly used in Go language to control text output is fmt

The functions mainly used for output in fmt are:

  • Print: output to the console, does not accept any formatting operations
  • Println: output to the console and wrap
  • Printf: Formatted output, only formatted strings can be printed out, and only variables of string type can be directly output (other types cannot be output directly)
  • Sprintf: format and return a string without any output
  • Fprintf: to format and output to io.Writers instead of os.Stdout

format

Use the Printf function to test the string formatting in the Go language:

fmt.Sprintf(格式化样式, 参数列表…)
  • Format style: string format, format symbol starts with %, %s string format, %d decimal integer format
  • Parameter list: Multiple parameters are separated by commas, and the number must correspond to the number in the formatting style, otherwise an error will be reported at runtime

for example:

username := "boy"
fmt.Printf("welcome, %s", username)

integer formatting

Placeholder describe
%b Integers displayed in binary
%The Integer displayed in octal
%d Integer displayed in decimal
%x Integer displayed in hexadecimal
%X Integers displayed in hexadecimal, uppercase
%c The character represented by the corresponding Unicode code point
%IN Unicode character, Unicode format: 123, equivalent to "U+007B"
func main() {
    
    
	fmt.Printf("%b \n", 123) //1111011
	fmt.Printf("%o \n", 123) //173
	fmt.Printf("%d \n", 123) //123
	fmt.Printf("%x \n", 123) //7b
	fmt.Printf("%X \n", 123) //7B   
	fmt.Printf("%c \n", 123) //{
    
    
	fmt.Printf("%U \n", 123) //U+007B 
}

Formatting floating point numbers

Placeholder describe
%and Scientific notation, such as 1.234560e+02
%AND Scientific notation, such as 1.234560E+02
%f with a decimal point and no exponent, e.g. 123.456
%F Equivalent to %f
%g Choose %e or %f as appropriate to produce more compact (no trailing 0) output
%G Choose %E or %F as appropriate to produce more compact (no trailing 0) output
func main() {
    
    
	fmt.Printf("%e \n", 123.456) //1.234560e+02
	fmt.Printf("%E \n", 123.456) //1.234560E+02
	fmt.Printf("%f \n", 123.456) //123.456000
	fmt.Printf("%F \n", 123.456) //123.456000
	fmt.Printf("%g \n", 123.456) //123.456
	fmt.Printf("%G \n", 123.456) //123.456 
}

Boolean formatting

Placeholder describe
%t true or false
func main() {
    
    
	fmt.Printf("%t", true) //true
}

character formatting

Placeholder describe
%c The character represented by the corresponding Unicode code point
func main() {
    
    
	fmt.Printf("%c", 0x4E2D) //中
}

string formatting

Placeholder describe
%s Output string or []byte directly
%q Strings surrounded by double quotes, safely escaped by Go syntax
%x Each byte is represented by a two-character hexadecimal number (using af)
%X Each byte is represented by a two-character hexadecimal number (using AF)
func main() {
    
    
	fmt.Printf("%s \n", "Hello world") //Hello world
	fmt.Printf("%q \n", "Hello world") //"Hello world"
	fmt.Printf("%x \n", "Hello world") //48656c6c6f20776f726c64
	fmt.Printf("%X \n", "Hello world") //48656C6C6F20776F726C64
}

pointer formatting

Placeholder describe
%p Represented as hexadecimal with leading 0x
%#p Represented as hexadecimal, without leading 0x
func main() {
    
    
	a := "Hello world"
	b := &a
	fmt.Printf("%p \n", b)  //0xc000046230
	fmt.Printf("%#p \n", b) //c000046230
}

generic placeholder

Placeholder describe
%in the default format for the value
%+v Similar to %v, but the field name will be added when outputting the structure
%#in Go-grammatical representation of the corresponding value
%T Go syntax representation of the type of the corresponding value
%% Percent sign, literal %, non-placeholder meaning
func main() {
    
    
	fmt.Printf("%v \n", "Hello World")   //Hello World
	fmt.Printf("%+v \n", "Hello World")  //Hello World
	fmt.Printf("%#v \n", "Hello World")  //"Hello World"
	fmt.Printf("%T \n", "Hello World")   //string
	fmt.Printf("%%%v \n", "Hello World") //%Hello World
}

Width representation

Floating point precision control
The width is specified by a decimal number immediately following the percent sign, if no width is specified, the value is represented without padding except where necessary.
Precision is specified by (optional) width followed by a period followed by a decimal number. If no precision is specified, the default precision will be used; if the dot is not followed by a number, the precision is 0. Examples are as follows

func main() {
    
    
	fmt.Printf("|%f|\n", 123.456)     //|123.456000|
	fmt.Printf("|%12f|\n", 123.456)   //|  123.456000|
	fmt.Printf("|%.3f|\n", 123.456)   //|123.456|
	fmt.Printf("|%12.3f|\n", 123.456) //|     123.456|
	fmt.Printf("|%12.f|\n", 123.456)  //|         123|
}

string length control

Width setting format: add a number in the middle of the placeholder, the number is positive or negative, +: right-aligned, -: left-aligned
Minimum width: percent sign followed by a decimal number, if the part is not enough, you can choose to fill with 0
Maximum width: after the decimal point Decimal number, the excess part will be truncated

func main() {
    
    
	fmt.Printf("|%s|\n", "123.456")    //|123.456|
	fmt.Printf("|%12s|\n", "123.456")  //|     123.456|
	fmt.Printf("|%-12s|\n", "123.456") //|123.456     |
	fmt.Printf("|%012s|\n", "123.456") //|00000123.456|
	fmt.Printf("|%.5s|\n", "123.456")  //|123.4|
}

Guess you like

Origin blog.csdn.net/shn111/article/details/127877743