加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang fmt格式化字符串%v,%T

发布时间:2020-12-16 18:04:14 所属栏目:大数据 来源:网络整理
导读: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 如下: b

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会打印实际类型的值。
举例说明如下。

example

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:%Tn",i)
        fmt.Printf("value:%vn",i)
        fmt.Printf("value+:%+vn",i)
        fmt.Printf("value#:%#vn",i)


        fmt.Println("========interface========")
        var interf interface{} = i
        fmt.Printf("%vn",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}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读