Printf、Sprintf、Fprintf三个函数的format参数中,均可放置占位符verb,来对对应数值进行格式化字符串。
通用verb
%v 值的默认格式表示
%+v 类似%v,但输出结构体时会添加字段名
%#v 值的Go语法表示
%T 值的类型的Go语法表示
%% 百分号
package main
import "fmt"
type Foo struct {
Fo int
Bar string
}
func main() {
foo := Foo{}
fmt.Printf("%v \n", foo)
fmt.Printf("%+v \n", foo)
fmt.Printf("%#v \n", foo)
fmt.Printf("%T \n", foo)
fmt.Printf("%% \n")
}
输出:
{0 }
{Fo:0 Bar:}
main.Foo{Fo:0, Bar:""}
main.Foo
%
Chan、Func、Map、Ptr、Slice、UnsafePointer
%p 内存地址,表示为十六进制,并加上前导的0x
package main
import "fmt"
type Foo struct {
bar int
}
func main() {
s := []string{}
fmt.Printf("%p \n", s)
m := map[string]string{}
fmt.Printf("%p \n", m)
c := make(chan int)
fmt.Printf("%p \n", c)
f := func() {}
fmt.Printf("%p \n", f)
i := 1
p := &i
fmt.Printf("%p \n", &i)
fmt.Printf("%p \n", p)
foo := Foo{}
fmt.Printf("%p \n", foo)
}
0x1195ab8
0xc000070180
0xc00006e060
0x109b260
0xc000016078
0xc000016078
%!p(main.Foo={0})