5. Formatted output in Go language

1. Universal formatted output

For formatted output in Go language, the fmt package is usually used. The common output format is as shown in the following table:

Output format Output content
%v The default format representation of the value
%+v Similar to %v, but field names will be added when outputting the structure
%#v Go syntax representation of value
%T Go syntax representation of value types
package main

import "fmt"

func main() {
	var s string = "ajwlf"
	var z int = 1
	var r rune = '一'

	fmt.Printf("s: %v\n", s)
	fmt.Printf("s: %+v\n", s)
	fmt.Printf("s: %#v\n", s)
	fmt.Printf("s: %T\n", s)

	fmt.Printf("z: %v\n", z)
	fmt.Printf("z: %T\n", z)

	fmt.Printf("r: %v\n", r)
	fmt.Printf("r: %T\n", r)
}


out:

s: ajwlf
s: ajwlf
s: "ajwlf"
s: string
z: 1
z: int
r: 19968
r: int32

2.Boolean type

Output format Output content
%t word true or false
package main
import "fmt"
func main() {
    var flag bool
    fmt.Printf("%T, %t \n", flag, flag)
    flag = true
    fmt.Printf("%T, %t \n", flag, flag)
}

// 结果
bool, false
bool, true

3. Integer type

Output format Output content
%b Represented as binary
%c The unicode code value corresponding to this value
%d Expressed as decimal
%8d It means that the length of the integer is 8. If it is less than 8, add a space before the value; if it exceeds 8, the actual value shall prevail.
%08d It means that the length of the integer is 8. If it is less than 8, add 0 in front of the value; if it exceeds 8, the actual value shall prevail.
%o Represented as octal
%q The Go language syntax character literal value corresponding to this value is enclosed in single quotes and will be represented by a safe escape when necessary.
%x Expressed in hexadecimal, use a~f
%X Expressed in hexadecimal, use A~F
%U Represented as unicode format: U+1234, equivalent to U+%04X
%p pointer type
package main
import "fmt"
func main() {
    fmt.Printf("%T, %d \n", 123, 123)
    fmt.Printf("%T, %5d \n", 123, 123)
    fmt.Printf("%T, %05d \n", 123, 123)
    fmt.Printf("%T, %b \n", 123, 123)
    fmt.Printf("%T, %o \n", 123, 123)
    fmt.Printf("%T, %c \n", 97, 97)
    fmt.Printf("%T, %q \n", 97, 97)
    fmt.Printf("%T, %x \n", 123, 123)
    fmt.Printf("%T, %X \n", 123, 123)
    fmt.Printf("%T, %U \n", '一', '一')
}

// 结果
int, 123
int,   123
int, 00123
int, 1111011
int, 173
int, a
int, 'a'
int, 7b
int, 7B
int32, U+4E00

4. Floating point type and complex number type

Output format Output content
%b Scientific notation without decimal part and binary exponent, such as -123456p-78
%e (=%.6e) Scientific notation with 6 decimal places, such as -1234.456e+78
%E Scientific notation, such as -1234.456E+78
%f (=%.6f) has 6 decimal places, such as 123.456123
%F Equivalent to %f
%g Use %e or %f format according to actual situation (obtain more concise and accurate output)
%G Use %E or %F format according to actual situation (obtain more concise and accurate output)
package main
import "fmt"
func main() {
    fmt.Printf("%b \n", 123.123456)
    fmt.Printf("%f \n", 123.1)
    fmt.Printf("%.2f \n", 123.125456)
    fmt.Printf("%e \n", 123.123456)
    fmt.Printf("%E \n", 123.123456)
    fmt.Printf("%.1e \n", 123.123456)
    fmt.Printf("%F \n", 123.123456)
    fmt.Printf("%g \n", 123.123456)
    fmt.Printf("%G \n", 123.123456)
}

// 结果
8664042977533870p-46
123.100000
123.13
1.231235e+02
1.231235E+02
1.2e+02
123.123456
123.123456
123.123456

5. Strings and byte arrays

Output format Output content
%s Directly output string or byte array
%q The Go syntax string literal value corresponding to this value is enclosed in double quotes and will be safely escaped if necessary.
%x Each byte is represented by a two-character hexadecimal number, using a~f
%X Each byte is represented by a two-character hexadecimal number, using A~F
package main
import "fmt"
func main() {
    arr := []byte{'x', 'y', 'z', 'z'}
    fmt.Printf("%s \n", "欢迎访问")
    fmt.Printf("%q \n", "欢迎访问")
    fmt.Printf("%x \n", "欢迎访问")
    fmt.Printf("%X \n", "欢迎访问")
    fmt.Printf("%T, %s \n", arr, arr)
    fmt.Printf("%T, %q \n", arr, arr)
    fmt.Printf("%T, %x \n", arr, arr)
    fmt.Printf("%T, %X \n", arr, arr)
}

//
欢迎访问
"欢迎访问"
e6aca2e8bf8ee8aebfe997aee5beaee5ada6e88b91
E6ACA2E8BF8EE8AEBFE997AEE5BEAEE5ADA6E88B91
[]uint8, xyzz
[]uint8, "xyzz"
[]uint8, 78797a7a
[]uint8, 78797A7A 

Guess you like

Origin blog.csdn.net/qq_40893490/article/details/127770219