jsrosetta

I/O

Reading from Stdin

How Node.js's readline/promises compares to bufio.Reader (Go), io::stdin (Rust), readLine() (Swift), and IO.readln (Java) for reading a line of input.

Minimum versions
Node.js ≥ 17Go ≥ 1.0Rust ≥ 1.58Swift ≥ 2.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

Reading interactive input means waiting for a line of text that ends with Enter. Node.js has the readline/promises module so you can await the answer, Go uses a bufio.Reader that reads up to the newline character, Rust reads through io::stdin().read_line(), Swift has a built-in global readLine() function, and Java 25 has IO.readln(prompt) — printing the prompt and reading a line in a single call.

Reading a line from stdin

import { createInterface } from 'node:readline/promises'
 
const rl = createInterface({ input: process.stdin, output: process.stdout })
 
const name = await rl.question('Enter name: ')
console.log('Your name is: ' + name)
 
rl.close()
Enter name: bob
Your name is: bob

Reference: github.com/miguelmota/golang-for-nodejs-developers#stdin