You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the type with Stringer interface custom formatting works only if printing directly this class, and doesn't work if it is a struct member:
type Frac struct {
a int
b int
}
func (f Frac) String() string { return fmt.Sprintf("\"%d/%d\"", f.a, f.b) }
func main() {
f := Frac{1, 3}
fmt.Printf("Fraction is %v\n", f)
st := struct {
one Frac
two Frac
}{Frac{1, 3}, Frac{3, 5}}
fmt.Printf("Struct is %v\n", st)
}
What did you see happen?
printing type as fmt.Printf("Fraction is %v\n", f) calls String() method:
Fraction is "1/3"
printing struct fmt.Printf("Struct is %v\n", st) does not:
Struct is {{1 3} {3 5}}
What did you expect to see?
libexec/src/fmt/print.go:pp.printArg checks for the Printf argument type and if it doesn't have custom formatting methods it goes to print it as value, while struct fields can have their own Stringer/GoStringer implementations
The text was updated successfully, but these errors were encountered:
As a developer of security-related module I think all my types are logging-safe.
But in other modules they can just print everything to logs in plain-text.
Go version
go 1.22
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/gFzyGuJGwGq
For the type with
Stringer
interface custom formatting works only if printing directly this class, and doesn't work if it is a struct member:What did you see happen?
printing type as
fmt.Printf("Fraction is %v\n", f)
callsString()
method:printing struct
fmt.Printf("Struct is %v\n", st)
does not:What did you expect to see?
libexec/src/fmt/print.go:
pp.printArg
checks for thePrintf
argument type and if it doesn't have custom formatting methods it goes to print it as value, while struct fields can have their own Stringer/GoStringer implementationsThe text was updated successfully, but these errors were encountered: