語言-格式輸入輸出中“%d, %s,%o,%x,%e,%f,%v,%+v,%#v”等的含義
%d整型輸出,%ld長整型輸出,
%s用來輸出一個字符串,
%+v 采取默認值輸出
%c用來輸出一個字符,
%u以十進制數輸出unsigned型數據(無符號數)
%f用來輸出,以小數形式輸出,(備注:浮點數是不能定義如的精度的,所以“%6.2f”這種寫法是“錯誤的”!!!)
%o以八進制數形式輸出整數,
%x以十六進制數形式輸出整數,
%e以指數形式輸出實數,
T常用的格式化字符串有:
%v the value in a default format
when printing structs, the plus flag (%+v) adds field names
%#v a Go-syntax representation of the value
%T a Go-syntax representation of the type of the value
不同類型默認的%v 如下:
bool: %t
int, int8 etc.: %d
uint, uint8 etc.: %d, %#x if printed with %#v
float32, complex64, etc: %g
string: %s
chan: %p
pointer: %p
對於interface{}, %v會打印實際類型的值。
eg:
package main
import (
"fmt"
)
type Power struct{
age int
high int
name string
}
func main() {
var i Power = Power{age: 10, high: 178, name: "NewMan"}
fmt.Printf("type:%T\n", i)
fmt.Printf("value:%v\n", i)
fmt.Printf("value+:%+v\n", i)
fmt.Printf("value#:%#v\n", i)
fmt.Println("========interface========")
var interf interface{} = i
fmt.Printf("%v\n", interf)
fmt.Println(interf)
}
output:
type:main.Power
value:{10 178 NewMan}
value+:{age:10 high:178 name:NewMan}
value#:main.Power{age:10, high:178, name:”NewMan”}
========interface========
{10 178 NewMan}
{10 178 NewMan}