Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #278 Read one line from the standard input

Read one line into the string line.

Explain what happens if EOF is reached.

import "bufio"
import "os"
s := bufio.NewScanner(os.Stdin)
if ok := s.Scan(); !ok {
	log.Fatal(s.Err())
}
line := s.Text()

This handles any error (including EOF) by aborting the program execution.

WARNING: this works only for lines smaller than 64kB each.
with Ada.Text_IO;
Line : constant String := Ada.Text_IO.Get_Line;

End_Error is raised if EOF is reached.

New implementation...
< >
programming-idioms.org