golang语言-格式输入输出中“%d, %s,%o,%x,%e,%f,%v,%+v,%#v”等的含义

语言-格式输入输出中“%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}

猜你喜欢

转载自www.cnblogs.com/show58/p/12396739.html
今日推荐