Skip to content
Tutorials Center
My learning

Topics

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.

Go
var name string
var age int
n, err := fmt.Sscan("Ann 31", &name, &age)

Try it

Go Scan from standard input

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.

Go Sscan, Sscanf and errors

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

Go
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: nil if all values were read; io.EOF if input ended before the first value; io.ErrUnexpectedEOF if it ended in the middle; or a parse error such as "expected integer".

Usage notes

  • A string operand receives one whitespace-delimited word. You cannot read a whole line with Scan; use bufio.Scanner or bufio.Reader.ReadString('\n').
  • Scanln requires the values to be on one line and a newline or EOF right after the last. Extra tokens give "expected newline".
  • Scanf treats newlines in the input strictly: the format must contain \n to 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 err and break, or you may spin forever on the same bad token.
  • Each Scan call on os.Stdin reads byte by byte without buffering, which is very slow for large inputs. For a million numbers, use bufio.NewReader(os.Stdin) with Fscan.
  • Always pass pointers. Passing a value instead of a pointer returns an error like "can't scan type".
  • For structured data, strconv.Atoi and strings.Fields give more precise error messages.