I/O
Writing to Stdout and Stderr
How Node.js's process.stdout.write/process.stderr.write compares to fmt.Fprint (Go), io::Write (Rust), FileHandle (Swift), and System.out/err.write (Java).
- Minimum versions
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.0Swift ≥ 3.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.
console.log/console.error covers most needs, but sometimes you need to write straight to a stream without an extra newline or formatting. All five languages let you write directly to stdout/stderr as a stream/writer: Go uses fmt.Fprint, Rust uses the io::Write trait, Swift uses FileHandle, and Java writes raw bytes through System.out/System.err (both a PrintStream).
Writing to stdout
process.stdout.write('hello world\n')package main
import (
"fmt"
"os"
)
func main() {
fmt.Fprint(os.Stdout, "hello world\n")
}use std::io::{self, Write};
fn main() {
// .write_all() with a byte string instead of write!(...,"…\n") avoids
// tripping both the write_with_newline and explicit_write clippy lints
io::stdout().write_all(b"hello world\n").unwrap();
}import Foundation
try FileHandle.standardOutput.write(contentsOf: "hello world\n".data(using: .utf8)!)void main() throws Exception {
System.out.write("hello world\n".getBytes());
System.out.flush();
}hello worldWriting to stderr
process.stderr.write('hello error\n')package main
import (
"fmt"
"os"
)
func main() {
fmt.Fprint(os.Stderr, "hello error\n")
}use std::io::{self, Write};
fn main() {
io::stderr().write_all(b"hello error\n").unwrap();
}import Foundation
try FileHandle.standardError.write(contentsOf: "hello error\n".data(using: .utf8)!)void main() throws Exception {
System.err.write("hello error\n".getBytes());
System.err.flush();
}hello errorReference: github.com/miguelmota/golang-for-nodejs-developers#stdout