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()package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter name: ")
text, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
name := strings.TrimSpace(text)
fmt.Printf("Your name is: %s\n", name)
}use std::io::{self, Write};
fn main() {
print!("Enter name: ");
io::stdout().flush().unwrap(); // print! doesn't auto-flush, so flush before reading
let mut name = String::new();
io::stdin().read_line(&mut name).unwrap();
let name = name.trim(); // read_line keeps the trailing '\n'
println!("Your name is: {name}");
}print("Enter name: ", terminator: "")
let name = readLine() ?? ""
print("Your name is: \(name)")void main() {
String name = IO.readln("Enter name: ");
IO.println("Your name is: " + name);
}Enter name: bob
Your name is: bobReference: github.com/miguelmota/golang-for-nodejs-developers#stdin