Go Package fmt: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 23: Line 23:
{{Internal|Go_Language_Error_Handling#Wrapping_Errors|Error Handling | Wrapping Errors}}
{{Internal|Go_Language_Error_Handling#Wrapping_Errors|Error Handling | Wrapping Errors}}
=Interfaces=
=Interfaces=
==<tt>fmt.Stringer</tt>==
<syntaxhighlight lang='go'>
type Stringer interface {
  String() string
}
</syntaxhighlight>
<code>Stringer</code> is implemented by any type that has a <code>String()</code> method, which defines the "native" format for that value. The <code>String()</code> method is used to print values passed as an operand to any format that accepts a string or to an unformatted printer such as <code>Print</code>.

Revision as of 20:31, 15 December 2023

External

Internal

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() Format String

Printf(), Println()

Printing to stdout and stderr

Scanf(), Scanln()

Handling stdin in Go

Errorf()

Error Handling | Wrapping Errors

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.