fmt Module
The fmt module provides functions for formatting and printing output to the console.
Functions
Println(args...)
Prints the given arguments to the console, followed by a newline character.
Parameters: - args...: A variable number of arguments of any type.
Returns: - (None, error): This function does not return a value, but it follows the standard error handling pattern.
Example:
| Println Example | |
|---|---|
Print(args...)
Prints the given arguments to the console without a newline character at the end.
Parameters: - args...: A variable number of arguments of any type.
Returns: - (None, error): This function does not return a value, but it follows the standard error handling pattern.
Example:
| Print Example | |
|---|---|
Printf(format, args...)
Prints formatted output to the console. It uses a format string and a variable number of arguments.
Parameters: - format: A string that contains format specifiers. - args...: The arguments to be formatted.
Returns: - (None, error): This function does not return a value, but it follows the standard error handling pattern.
Format Specifiers: - %v: The default format for the value. - %s: A string. - %d: A decimal integer. - %f: A floating-point number.
Example:
| Printf Example | |
|---|---|
Errorf(format, args...)
Creates a new error with a formatted error message.
Parameters: - format: A string that contains format specifiers. - args...: The arguments to be formatted.
Returns: - (error, None): A new error object.
Example:
Sprint(args...)
Builds and returns a string by concatenating the default format of each argument separated by spaces. No trailing newline.
Parameters: - args...: Arguments to concatenate.
Returns: - string
Example:
| Sprint Example | |
|---|---|
Sprintln(args...)
Like Sprint but appends a trailing newline to the result.
Parameters: - args...: Arguments to concatenate.
Returns: - string
Example:
| Sprintln Example | |
|---|---|
Sprintf(format, args...)
Formats according to a format specifier and returns the resulting string.
Parameters: - format: Format string (supports %s, %d, %f, %t, %v, %+v, %#v, %T). - args...: Arguments to format.
Returns: - string
If the first argument is not a string or there are fewer arguments than required by the format string, a runtime error is raised.
Example:
| Sprintf Format | |
|---|---|
Stdout() and Stderr()
Return handles that represent standard output and standard error. These handles can be used with Fprint, Fprintln, and Fprintf.
Returns: - (any, error)
Example:
| Stdout and Stderr | |
|---|---|
Fprint(w, args...)
Writes arguments to the given writer handle without a trailing newline.
Parameters: - w: Writer handle (use fmt.Stdout() or fmt.Stderr()). - args...: Values to write.
Returns: - (None, error)
Example:
| Fprint to Writer | |
|---|---|
Fprintln(w, args...)
Writes arguments to the writer followed by a newline.
Parameters: - w: Writer handle. - args...: Values to write.
Returns: - (None, error)
Example:
| Fprintln to Writer | |
|---|---|
Fprintf(w, format, args...)
Formats according to a format specifier and writes to the writer.
Parameters: - w: Writer handle. - format: Format string. - args...: Arguments to format.
Returns: - (None, error)
Example:
| Fprintf to Writer | |
|---|---|
Scan()
Reads a line from standard input and splits it by whitespace into fields.
Returns: - (array, error)
Scanln()
Reads a line from standard input and returns it as a string (without trailing newline).
Returns: - (string, error)
Scanf(format)
Reads a line from standard input and parses it according to the format string. Supported verbs: %s, %d, %f, %t.
Parameters: - format: Format string.
Returns: - (array, error)
Note: The
Scan*functions block waiting for input. Use them interactively or in dedicated examples.