Go Reference ยท fmt Package
Go fmt.Scan() family
Reference for Go fmt.Scan, Scanln, Scanf, Sscan, Sscanf and Fscan: reading whitespace-separated values from stdin, strings and readers, with error handling.
The Scan functions parse text into variables whose pointers you pass in. They are the simplest way to read numbers and words from standard input or from a string. Three families exist: Scan, Scanln and Scanf read from standard input; Sscan... read from a string; Fscan... read from any io.Reader.
Within a family, the plain version treats newlines like spaces, the ln version stops at a newline, and the f version follows a format string. The input is split into tokens by spaces, and each token is converted to the type of the pointer it is assigned to.
var name string var age int n, err := fmt.Sscan("Ann 31", &name, &age)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func Scan(a ...any) (n int, err error) func Scanln(a ...any) (n int, err error) func Scanf(format string, a ...any) (n int, err error) func Sscan(str string, a ...any) (n int, err error) func Sscanln(str string, a ...any) (n int, err error) func Sscanf(str string, format string, a ...any) (n int, err error) func Fscan(r io.Reader, a ...any) (n int, err error) func Fscanln(r io.Reader, a ...any) (n int, err error) func Fscanf(r io.Reader, format string, a ...any) (n int, err error)
| Name | Description |
|---|---|
a |
Pointers to variables: *int, *string, *float64, *bool, *[]byte and so on, or any type implementing fmt.Scanner. |
str, r |
The source: a string or an io.Reader. |
format |
A format using verbs such as %d, %s, %f, %q, %c, %x, %v; spaces and literal text must match the input. |
Returns
n: how many values were stored successfully.err:nilif all values were read;io.EOFif input ended before the first value;io.ErrUnexpectedEOFif it ended in the middle; or a parse error such as "expected integer".
Usage notes
- A
stringoperand receives one whitespace-delimited word. You cannot read a whole line withScan; usebufio.Scannerorbufio.Reader.ReadString('\n'). Scanlnrequires the values to be on one line and a newline or EOF right after the last. Extra tokens give "expected newline".Scanftreats newlines in the input strictly: the format must contain\nto match one. This trips up many programs that read several lines in a loop.- A failed scan consumes part of the input. In a loop, check
errand break, or you may spin forever on the same bad token. - Each
Scancall onos.Stdinreads byte by byte without buffering, which is very slow for large inputs. For a million numbers, usebufio.NewReader(os.Stdin)withFscan. - Always pass pointers. Passing a value instead of a pointer returns an error like "can't scan type".
- For structured data,
strconv.Atoiandstrings.Fieldsgive more precise error messages.