Reading Input
Reading Input
Thagore provides built-in and standard library functions for reading from stdin. The built-in input() function reads a line from the console, and the io module adds prompts and bulk-read helpers.
Built-in: input()
The input() function is a language built-in — no import needed. It reads one line from standard input and returns it as a String. The trailing newline is stripped automatically.
func main() -> i32: print("What is your name?") let name = input() print("Hello, " + name + "!") return 0What is your name?> MinhHello, Minh!Built-in: input() with i32 parse
To read an integer, read the string first then parse with str.parse_i32(), or use io.input_i32() from the io module:
import "lib/io.tg" as io
func main() -> i32: print("Enter a number:") let n = io.input_i32() print(n * 2) return 0Enter a number:> 2142io Module Functions
Import the io module for all input helpers:
import "lib/io.tg" as ioFunction Reference
| Function | Return | Description |
|---|---|---|
io.input_prompt(prompt) | String | Print a prompt then read a line from stdin |
io.input_prompt() | String | Read a line with no prompt (same as input()) |
io.input_i32() | i32 | Read a line and parse it as a signed integer |
io.stdin_read_line(prompt) | String | Alias for input_prompt |
io.stdin_read_all() | String | Read entire stdin into a single string |
io.stdread(path) | String | Read from a file or from stdin if path is "", "-", or "stdin" |
io.input_prompt() — Prompt + Read
Shows a prompt on the same line, then waits for input:
import "lib/io.tg" as ioimport "lib/string.tg" as str
func main() -> i32: let name = str.trim(io.input_prompt("Enter your name: ")) let age_s = str.trim(io.input_prompt("Enter your age: ")) print("Hello " + name + "! You entered age: " + age_s) return 0Enter your name: MinhEnter your age: 22Hello Minh! You entered age: 22io.stdin_read_all() — Read All of Stdin
Reads everything from stdin into a single String. Useful when piping input into a program:
import "lib/io.tg" as io
func main() -> i32: let text = io.stdin_read_all() print("You sent " + text) return 0echo "hello world" | ./echo_all# You sent hello worldio.stdread() — File or Stdin
stdread(path) reads from a file path, or from stdin if the path is one of the standard stdin selectors:
| Path value | Behaviour |
|---|---|
"" (empty string) | reads stdin |
"-" | reads stdin |
"stdin" / "STDIN" | reads stdin |
"con" / "CON" | reads stdin (Windows console) |
"/dev/stdin" | reads stdin |
| any other path | reads that file |
import "lib/io.tg" as ioimport "lib/env.tg" as env
func main() -> i32: let path = env.get(1) let text = io.stdread(path) print(text) return 0./cat myfile.txt # reads myfile.txt./cat - # reads stdin./cat # reads stdin (empty path)Reading Multiple Lines (Loop)
Use a loop with input() to read until EOF:
import "lib/string.tg" as str
func main() -> i32: let total = 0 let line = input() while (str.len(line) > 0): let n = str.parse_i32(line) total = total + n line = input() print(total) return 0printf "10\n20\n30\n" | ./sum_lines# 60Practical Example: Simple Calculator
import "lib/io.tg" as ioimport "lib/string.tg" as str
func main() -> i32: let a = str.parse_i32(str.trim(io.input_prompt("First number: "))) let b = str.parse_i32(str.trim(io.input_prompt("Second number: "))) print("Sum: " + str.from_i32(a + b)) print("Diff: " + str.from_i32(a - b)) print("Product: " + str.from_i32(a * b)) return 0First number: 10Second number: 3Sum: 13Diff: 7Product: 30Summary
| Use case | Code |
|---|---|
| Read a string line (no prompt) | let s = input() |
| Read a string with prompt | let s = io.input_prompt("Enter: ") |
| Read an integer | let n = io.input_i32() |
| Read all of stdin | let text = io.stdin_read_all() |
| Read file or stdin | let text = io.stdread(path) |