Go Package fmt: Difference between revisions
Jump to navigation
Jump to search
Line 8: | Line 8: | ||
==Conversion Characters== | ==Conversion Characters== | ||
===<tt>%v</tt>=== | ===<tt>%v</tt>=== | ||
Render the value as string in the default format. | |||
===<tt>%+v</tt>=== | ===<tt>%+v</tt>=== | ||
When printing structs, the plus flag <code>%+v</code> adds field names. | When printing structs, the plus flag <code>%+v</code> adds field names. | ||
Line 17: | Line 17: | ||
===<tt>%%</tt>=== | ===<tt>%%</tt>=== | ||
A literal percent sign. Consumes no value. | A literal percent sign. Consumes no value. | ||
===Strings=== | |||
<syntaxhighlight lang='go'> | |||
color := "blue" | |||
fmt.Printf("The color is %s\n", color) | |||
</syntaxhighlight> | |||
===Integers=== | |||
<syntaxhighlight lang='go'> | |||
size := 1 | |||
fmt.Printf("The size is %d\n", size) | |||
</syntaxhighlight> | |||
===Characters=== | |||
<syntaxhighlight lang='go'> | |||
c := 'x' | |||
fmt.Printf("The character is %c\n", c) | |||
</syntaxhighlight> | |||
===Booleans=== | |||
<syntaxhighlight lang='go'> | |||
b := true | |||
fmt.Printf("The boolean value is %t\n", b) | |||
</syntaxhighlight> | |||
===Floating Point Numbers=== | |||
<syntaxhighlight lang='go'> | |||
f := 1.0 | |||
fmt.Printf("The floating point value is %f\n", f) | |||
</syntaxhighlight> | |||
===Pointers=== | |||
{{Internal|Pointers_in_Go#Displaying_Pointers|Pointers in Go | Displaying Pointers}} | |||
===Structs=== | |||
{{Internal|Go_Structs#Printing_Structs|Go Structs | Printing Structs}} | |||
==Leading Zero Padding for Integers== | ==Leading Zero Padding for Integers== | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> |
Revision as of 21:57, 22 December 2023
External
Internal
Formatting
Conversion Characters
%v
Render the value as string in the default format.
%+v
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.
%%
A literal percent sign. Consumes no value.
Strings
color := "blue"
fmt.Printf("The color is %s\n", color)
Integers
size := 1
fmt.Printf("The size is %d\n", size)
Characters
c := 'x'
fmt.Printf("The character is %c\n", c)
Booleans
b := true
fmt.Printf("The boolean value is %t\n", b)
Floating Point Numbers
f := 1.0
fmt.Printf("The floating point value is %f\n", f)
Pointers
Structs
Leading Zero Padding for Integers
i := 7
s := fmt.Sprintf("%06d", i) // will produce "000007" (five zeroes)
Leading Space Padding for Integer
i := 7
s := fmt.Sprintf("%6d", i) // will produce " 7" (five spaces)
Functions
Sprintf()
Format a string and returns it as a result:
message := fmt.Sprintf("Hi, %v. Welcome!", name)
For more details on the format string, see:
Printf(), Println()
Scanf(), Scanln()
Errorf()
Interfaces
fmt.Stringer
type Stringer interface {
String() string
}
Stringer
is implemented by any type that has a String()
method, which defines the "native" format for that value. The String()
method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as Print
. For a usage example, see: