Skip to content

input Module

The input module provides buffered console input utilities (built on Go's bufio over os.Stdin). All functions follow Harneet's convention of returning (result, error) tuples.

When an input parse fails (e.g., invalid integer), the function returns (None, error) with a helpful message.

Functions

ReadLine()

Reads a single line from standard input and returns it without the trailing newline.

  • Parameters: none
  • Returns: (string, error)

Example:

Read Line
1
2
3
4
5
6
7
8
9
import input, fmt

fmt.Println("Enter a line:")
var line, err = input.ReadLine()
if err != None {
    fmt.Println("Error:", err)
} else {
    fmt.Println("You entered:", line)
}


ReadWord()

Reads the first whitespace-delimited token from the next line of input.

  • Parameters: none
  • Returns: (string, error)

Example:

Read Word
1
2
3
4
5
6
7
8
9
import input, fmt

fmt.Println("Enter some words:")
var w, err = input.ReadWord()
if err != None {
    fmt.Println("Error:", err)
} else {
    fmt.Println("First word:", w)
}


ReadWords()

Reads a line from standard input and splits it into whitespace-delimited tokens.

  • Parameters: none
  • Returns: (array, error) (array of strings)

Example:

Read Words
1
2
3
4
5
6
7
8
9
import input, fmt

fmt.Println("Enter some words:")
var words, err = input.ReadWords()
if err != None {
    fmt.Println("Error:", err)
} else {
    fmt.Println("Words:", words)
}


ReadInt()

Reads a line and parses it as a base-10 integer.

  • Parameters: none
  • Returns: (int, error)

Example:

Read Integer
1
2
3
4
5
6
7
8
9
import input, fmt

fmt.Println("Enter an integer:")
var iv, err = input.ReadInt()
if err != None {
    fmt.Println("Error:", err)
} else {
    fmt.Println("You entered:", iv)
}


ReadFloat()

Reads a line and parses it as a float64.

  • Parameters: none
  • Returns: (float64, error)

Example:

Read Float
1
2
3
4
5
6
7
8
9
import input, fmt

fmt.Println("Enter a float:")
var fv, err = input.ReadFloat()
if err != None {
    fmt.Println("Error:", err)
} else {
    fmt.Println("You entered:", fv)
}


ReadBool()

Reads a line and parses it as a boolean. Accepted values (case-insensitive): true, t, 1, yes, y, false, f, 0, no, n.

  • Parameters: none
  • Returns: (bool, error)

Example:

Read Boolean
1
2
3
4
5
6
7
8
9
import input, fmt

fmt.Println("Enter a boolean (true/false, yes/no, 1/0):")
var bv, err = input.ReadBool()
if err != None {
    fmt.Println("Error:", err)
} else {
    fmt.Println("You entered:", bv)
}


Prompt(message string)

Prints a prompt message to stdout and reads a single line from standard input.

  • Parameters:
  • message (string): prompt to display before reading input
  • Returns: (string, error)

Example:

Prompt Input
import input, fmt

var name, err1 = input.Prompt("Name: ")
if err1 != None {
    fmt.Println("Error:", err1)
}

var age, err2 = input.Prompt("Age: ")
if err2 != None {
    fmt.Println("Error:", err2)
}

fmt.Println("Hello", name, "you are", age, "years old!")