String() Method and Format Specifiers
This lesson describes how to use format specifiers and change the String() function to bring variations to the default print functions in Go.
When you define a type with a lot of methods, chances are you will want to make a customized string-output for it with the String( )
method, in other words: a human-readable and printable output. This is because if String( )
is defined for a certain type, then this method will be used in fmt.Printf()
to produce the default output, which is the output produced with the format specifier %v. Furthermore, fmt.Print()
and fmt.Println()
will automatically use the String( )
method.
We will test this out with the help of a program:
Get hands-on with 1400+ tech skills courses.