Detailed explanation of placeholders in Golang

1. %v: Value placeholder, format the value into a more compact representation.

package main

import (
	"fmt"
)

type user struct {
	name string
	age  int
}

func main() {
	u := user{name: "路多辛", age: 18}
	fmt.Printf("The value is: %v\n", u)

	m := map[string]interface{}{"name": "路多辛", "age": 18}
	fmt.Printf("The value is: %v\n", m)

	fmt.Printf("The value is: %v\n", 666)
}

The output is as follows:

The value is: {路多辛 18}
The value is: map[age:18 name:路多辛]
The value is: 666

2. %+v: Compared with %v, if it is a structure, it will bring the field name.

package main

import (
	"fmt"
)

type user struct {
	name string
	age  int
}

func main() {
	u := user{name: "路多辛", age: 18}
	fmt.Printf("The value is: %+v\n", u)

	m := map[string]interface{}{"name": "路多辛", "age": 18}
	fmt.Printf("The value is: %+v\n", m)

	fmt.Printf("The value is: %+v\n", 666)
}

The output is as follows:

The value is: {name:路多辛 age:18}
The value is: map[age:18 name:路多辛]
The value is: 666

3. %#v: Go syntax representation of the value.

package main

import (
	"fmt"
)

type user struct {
	name string
	age  int
}

func main() {
	u := user{name: "路多辛", age: 18}
	fmt.Printf("The value is: %#v\n", u)

	m := map[string]interface{}{"name": "路多辛", "age": 18}
	fmt.Printf("The value is: %#v\n", m)

	fmt.Printf("The value is: %#v\n", 666)
}

The output is as follows:

The value is: main.user{name:"路多辛", age:18}
The value is: map[string]interface {}{"age":18, "name":"路多辛"}
The value is: 666

4. %T: Go syntax representation of the type of value.

package main

import (
	"fmt"
)

type user struct {
	name string
	age  int
}

func main() {
	u := user{name: "路多辛", age: 18}
	fmt.Printf("The value is: %T\n", u)

	m := map[string]interface{}{"name": "路多辛", "age": 18}
	fmt.Printf("The value is: %T\n", m)

	fmt.Printf("The value is: %T\n", 666)
}

The output is as follows:

The value is: main.user
The value is: map[string]interface {}
The value is: int

5. %%: Output a literal percent sign.

package main

import (
	"fmt"
)

func main() {
	fmt.Printf("%%") // 输出 %
}

6. %t: the word true or false.

package main

import (
	"fmt"
)

func main() {
	fmt.Printf("%t", true) // 输出 true
}

7. %b: binary representation.

package main

import (
	"fmt"
)

func main() {
	fmt.Printf("%b", 3) // 输出:11
}

8. %c: The corresponding Unicode code value.

package main

import (
	"fmt"
)

func main() {
	fmt.Printf("%c%c%c的所思所想", 0x8DEF, 0x591A, 0x8F9B)
  // 输出 路多辛的所思所想
}

9. %d: Decimal representation.

package main

import (
	"fmt"
)

func main() {
	fmt.Printf("%d", 0x12) // 输出 18
}

10. %o: octal representation.

package main

import (
	"fmt"
)

func main() {
	fmt.Printf("%o", 9) // 输出 11
}

11. %O: octal representation, with 0o prefix.

package main

import (
	"fmt"
)

func main() {
	fmt.Printf("%O", 9) // 输出 0o11
}

12. %q: Format the value as a character with a single quote.

package main

import "fmt"

func main() {
	fmt.Printf("%q", 0x8DEF) // 输出 '路'
}

13. %x: Format the value as an unsigned hexadecimal character.

package main

import "fmt"

func main() {
	fmt.Printf("%x\n", 0x8DEF) // 输出 8def
	fmt.Printf("%x", 13) // 输出 d
}

14. %X: The difference from %x is that the value is formatted as an uppercase letter.

package main

import "fmt"

func main() {
	fmt.Printf("%x\n", 0x8DEF) // 输出 8DEF
	fmt.Printf("%x", 13) // 输出 D
}

15. %U: Format the value as a string with a prefix "U+", in Unicode format.

package main

import "fmt"

func main() {
	fmt.Printf("%U\n", 0x8DEF) // 输出 U+8DEF
	fmt.Printf("%U", 13) // 输出 U+000D
}

16. %e: Format the value as a scientific notation representation with a decimal point.

package main

import "fmt"

func main() {
	fmt.Printf("The value is: %e\n", 10.1) // 输出 1.010000e+01
}

17. %E formats the value as a scientific notation representation with a decimal point.

package main

import "fmt"

func main() {
	fmt.Printf("The value is: %E\n", 10.1) // 输出 1.010000E+01
}

18. %f: Format the value as a floating-point representation with a decimal point.

package main

import "fmt"

func main() {
	fmt.Printf("The value is: %f\n", 10.1) //  输出 10.100000
}

19. %F: Same as %f.

package main

import "fmt"

func main() {
	fmt.Printf("The value is: %F\n", 10.1) // 输出 10.100000
}

20. %g: Formats the value as a string with a suitable representation, and can automatically select a floating-point representation with a decimal point or a floating-point representation without a decimal point.

package main

import "fmt"

func main() {
	fmt.Printf("The value is: %g\n", 10) //  输出 %!g(int=10)
	fmt.Printf("The value is: %g\n", 10.1) //  输出 10.1
  fmt.Printf("The value is: %g\n", 1000000.1) // 输出 1.0000001e+06
}

21. %G: Different from %g, %G will choose to use uppercase or lowercase letters according to the value range.

package main

import "fmt"

func main() {
	fmt.Printf("The value is: %G\n", 10) // 输出 %!G(int=10)
	fmt.Printf("The value is: %G\n", 10.1) // 输出 10.1
	fmt.Printf("The value is: %G\n", 1000000.1) // 输出 1.0000001E+06
}

22. %s string representation.

package main

import "fmt"

func main() {
	fmt.Printf("The value is: %s\n", "路多辛的博客") // 输出 路多辛的博客
	fmt.Printf("The value is: %s\n", []byte("路多辛")) // 输出 路多辛
}

23. %p: Format the value as the address representation of a pointer, in hexadecimal notation, prefixed with 0x.

package main

import "fmt"

func main() {
	a := 10
	fmt.Printf("The value is: %p\n", &a) // 输出例如 0xc0000b2008
}

24. %.(Number)s: Intercept the string of the specified length.

package main

import "fmt"

func main() {
	fmt.Printf("The value is: %.3s\n", "路多辛的博客") // 输出 路多辛
}

25. %w: Format the value as a string of an error message.

package main

import (
	"errors"
	"fmt"
)

func main() {
	w := fmt.Errorf("wrapD的错误信息-%w", errors.New("原始错误信息"))
	fmt.Println(w.Error()) // 输出 wrap的错误信息-原始错误信息
}

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131605167