Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AlexZ33 opened this issue May 25, 2021 · 0 comments
Open

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

AlexZ33 opened this issue May 25, 2021 · 0 comments

Comments

@AlexZ33
Copy link
Member

AlexZ33 commented May 25, 2021

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

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

// type:main.Power
// value:{10 178 NewMan}
// value+:{age:10 hight:178 name:NewMan}
// value#:main.Power{age:10, hight:178, name:"NewMan"}
// ========interface========
// {10 178 NewMan}
// {10 178 NewMan}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant