All packages

fmt

import "fmt"

Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler. # Printing There are four families of printing functions defined by their output destination. [Print], [Println] and [Printf] write to [os.Stdout]; [Sprint], [Sprintln] and [Sprintf] return a string; [Fprint], [Fprintln] and [Fprintf] write to an [io.Writer]; and [Append], [Appendln] and [Appendf] append the output to a byte slice. The functions w...

Go 1.26.1

Functions

func Append(b []byte, a ...any) []byte

Append formats using the default formats for its operands, appends the result to the byte slice, and returns the updated slice. Spaces are added between operands when neither is a string.

fmt/print.go:288

func Appendf(b []byte, format string, a ...any) []byte

Appendf formats according to a format specifier, appends the result to the byte slice, and returns the updated slice.

fmt/print.go:247

func Appendln(b []byte, a ...any) []byte

Appendln formats using the default formats for its operands, appends the result to the byte slice, and returns the updated slice. Spaces are always added between operands and a newline is appended.

fmt/print.go:331

func Errorf(format string, a ...any) error

Errorf formats according to a format specifier and returns the string as a value that satisfies error. If the format specifier includes a %w verb with an error operand, the returned error will implement an Unwrap method returning the operand. If there is more than one %w verb, the returned error will implement an Unwrap method returning a []error containing all the %w operands in the order they appear in the arguments. It is invalid to supply the %w verb with an operand that does not impleme...

fmt/errors.go:23

func FormatString(state State, verb rune) string

FormatString returns a string representing the fully qualified formatting directive captured by the [State], followed by the argument verb. ([State] does not itself contain the verb.) The result has a leading percent sign followed by any flags, the width, and the precision. Missing flags, width, and precision are omitted. This function allows a [Formatter] to reconstruct the original directive triggering the call to Format.

fmt/print.go:81

func Fprint(w io.Writer, a ...any) (int, error)

Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

fmt/print.go:260

func Fprintf(w io.Writer, format string, a ...any) (int, error)

Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered.

fmt/print.go:222

func Fprintln(w io.Writer, a ...any) (int, error)

Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

fmt/print.go:303

func Fscan(r io.Reader, a ...any) (int, error)

Fscan scans text read from r, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.

fmt/scan.go:121

func Fscanf(r io.Reader, format string, a ...any) (int, error)

Fscanf scans text read from r, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully parsed. Newlines in the input must match newlines in the format.

fmt/scan.go:141

func Fscanln(r io.Reader, a ...any) (int, error)

Fscanln is similar to [Fscan], but stops scanning at a newline and after the final item there must be a newline or EOF.

fmt/scan.go:130

func Print(a ...any) (int, error)

Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

fmt/print.go:271

func Printf(format string, a ...any) (int, error)

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.

fmt/print.go:232

func Println(a ...any) (int, error)

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

fmt/print.go:314

func Scan(a ...any) (int, error)

Scan scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.

fmt/scan.go:63

func Scanf(format string, a ...any) (int, error)

Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why. Newlines in the input must match newlines in the format. The one exception: the verb %c always scans the next rune in the input, even if it is a space (or tab etc.) or newline.

fmt/scan.go:80

func Scanln(a ...any) (int, error)

Scanln is similar to [Scan], but stops scanning at a newline and after the final item there must be a newline or EOF.

fmt/scan.go:69

func Sprint(a ...any) string

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.

fmt/print.go:277

func Sprintf(format string, a ...any) string

Sprintf formats according to a format specifier and returns the resulting string.

fmt/print.go:237

func Sprintln(a ...any) string

Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.

fmt/print.go:320

func Sscan(str string, a ...any) (int, error)

Sscan scans the argument string, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.

fmt/scan.go:99

func Sscanf(str string, format string, a ...any) (int, error)

Sscanf scans the argument string, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully parsed. Newlines in the input must match newlines in the format.

fmt/scan.go:113

func Sscanln(str string, a ...any) (int, error)

Sscanln is similar to [Sscan], but stops scanning at a newline and after the final item there must be a newline or EOF.

fmt/scan.go:105

Types

type Formatter interface {
	Format(f State, verb rune)
}

Formatter is implemented by any value that has a Format method. The implementation controls how [State] and rune are interpreted, and may call [Sprint] or [Fprint](f) etc. to generate its output.

fmt/print.go:54

type GoStringer interface {
	GoString() string
}

GoStringer is implemented by any value that has a GoString method, which defines the Go syntax for that value. The GoString method is used to print values passed as an operand to a %#v format.

fmt/print.go:71

type Scanner interface {
	Scan(state ScanState, verb rune) error
}

Scanner is implemented by any value that has a Scan method, which scans the input for the representation of a value and stores the result in the receiver, which must be a pointer to be useful. The Scan method is called for any argument to [Scan], [Scanf], or [Scanln] that implements it.

fmt/scan.go:55

type ScanState interface {
	// ReadRune reads the next rune (Unicode code point) from the input.
	// If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
	// return EOF after returning th...

ScanState represents the scanner state passed to custom scanners. Scanners may do rune-at-a-time scanning or ask the ScanState to discover the next space-delimited token.

fmt/scan.go:21

type State interface {
	// Write is the function to call to emit formatted output to be printed.
	Write(b []byte) (n int, err error)
	// Width returns the value of the width option and whether it h...

State represents the printer state passed to custom formatters. It provides access to the [io.Writer] interface plus information about the flags and options for the operand's format specifier.

fmt/print.go:39

type Stringer interface {
	String() string
}

Stringer is implemented by any value 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].

fmt/print.go:63