为什么不用%v打印int和string

Nothing bad will happen, but the %d verb instructs the fmt package to print is as a number (using base 10), and the %v verb means to use the default format which can be overridden.

See this example:

type MyInt int

func (mi MyInt) String() string {
    return fmt.Sprint("*", int(mi), "*")
}

func main() {
    var mi MyInt = 2
    fmt.Printf("%d %v", mi, mi)
}

Output:

2 *2*

你可能感兴趣的:(Golang)