fmt

Package fmt implements formatted I/O with functions analogous to C's printf and scanf.

Index

Functions

func Errorf

1func 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 implement the error interface. The %w verb is otherwise a synonym for %v.

The Errorf function lets us use formatting features to create descriptive error messages.

1const name, id = "bueller", 17
2err := fmt.Errorf("user %q (id %d) not found", name, id)
3fmt.Println(err.Error())

Output

user "bueller" (id 17) not found

func Fprint

1func Fprint(w io.Writer, a ...any) (n int, err 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.

1const name, age = "Kim", 22
2n, err := fmt.Fprint(os.Stdout, name, " is ", age, " years old.\n")
3
4if err != nil {
5	fmt.Fprintf(os.Stderr, "Fprint: %v\n", err)
6}
7fmt.Print(n, " bytes written.\n")

Output

Kim is 22 years old.
21 bytes written.

func Fprintf

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

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

1const name, age = "Kim", 22
2n, err := fmt.Fprintf(os.Stdout, "%s is %d years old.\n", name, age)
3
4if err != nil {
5	fmt.Fprintf(os.Stderr, "Fprintf: %v\n", err)
6}
7fmt.Printf("%d bytes written.\n", n)

Output

Kim is 22 years old.
21 bytes written.

func Fprintln

1func Fprintln(w io.Writer, a ...any) (n int, err 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.

1const name, age = "Kim", 22
2n, err := fmt.Fprintln(os.Stdout, name, "is", age, "years old.")
3
4if err != nil {
5	fmt.Fprintf(os.Stderr, "Fprintln: %v\n", err)
6}
7fmt.Println(n, "bytes written.")

Output

Kim is 22 years old.
21 bytes written.

func Fscan

1func Fscan(r io.Reader, a ...any) (n int, err 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.

func Fscanf

1func Fscanf(r io.Reader, format string, a ...any) (n int, err 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.

 1var (
 2	i	int
 3	b	bool
 4	s	string
 5)
 6r := strings.NewReader("5 true gophers")
 7n, err := fmt.Fscanf(r, "%d %t %s", &i, &b, &s)
 8if err != nil {
 9	fmt.Fprintf(os.Stderr, "Fscanf: %v\n", err)
10}
11fmt.Println(i, b, s)
12fmt.Println(n)

Output

5 true gophers
3

func Fscanln

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

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

 1s := `dmr 1771 1.61803398875
 2ken 271828 3.14159`
 3r := strings.NewReader(s)
 4var a string
 5var b int
 6var c float64
 7for {
 8	n, err := fmt.Fscanln(r, &a, &b, &c)
 9	if err == io.EOF {
10		break
11	}
12	if err != nil {
13		panic(err)
14	}
15	fmt.Printf("%d: %s, %d, %f\n", n, a, b, c)
16}

Output

3: dmr, 1771, 1.618034
3: ken, 271828, 3.141590

func Print

1func Print(a ...any) (n int, err 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.

1const name, age = "Kim", 22
2fmt.Print(name, " is ", age, " years old.\n")

Output

Kim is 22 years old.

func Printf

1func Printf(format string, a ...any) (n int, err 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.

1const name, age = "Kim", 22
2fmt.Printf("%s is %d years old.\n", name, age)

Output

Kim is 22 years old.

func Println

1func Println(a ...any) (n int, err 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.

1const name, age = "Kim", 22
2fmt.Println(name, "is", age, "years old.")

Output

Kim is 22 years old.

func Scan

1func Scan(a ...any) (n int, err 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.

func Scanf

1func Scanf(format string, a ...any) (n int, err 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.

func Scanln

1func Scanln(a ...any) (n int, err error)

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

func Sprint

1func 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.

1const name, age = "Kim", 22
2s := fmt.Sprint(name, " is ", age, " years old.\n")
3
4io.WriteString(os.Stdout, s)

Output

Kim is 22 years old.

func Sprintf

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

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

1const name, age = "Kim", 22
2s := fmt.Sprintf("%s is %d years old.\n", name, age)
3
4io.WriteString(os.Stdout, s)

Output

Kim is 22 years old.

func Sprintln

1func 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.

1const name, age = "Kim", 22
2s := fmt.Sprintln(name, "is", age, "years old.")
3
4io.WriteString(os.Stdout, s)

Output

Kim is 22 years old.

func Sscan

1func Sscan(str string, a ...any) (n int, err 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.

func Sscanf

1func Sscanf(str string, format string, a ...any) (n int, err 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.

1var name string
2var age int
3n, err := fmt.Sscanf("Kim is 22 years old", "%s is %d years old", &name, &age)
4if err != nil {
5	panic(err)
6}
7fmt.Printf("%d: %s, %d\n", n, name, age)

Output

2: Kim, 22

func Sscanln

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

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


© Matthias Hochgatterer – MastodonGithubRésumé