golang verb

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})

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章