I/O
Ghi ra Stdout và Stderr
process.stdout.write/process.stderr.write của Node.js so với fmt.Fprint (Go), io::Write (Rust), FileHandle (Swift) và System.out/err.write (Java).
- Phiên bản tối thiểu
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.0Swift ≥ 3.0Java ≥ 25
- Đã chạy thử trên
- Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1
Code Node.js là ES module: lưu file .mjs hoặc đặt "type": "module" trong package.json.
console.log/console.error đã đủ dùng hầu hết thời gian, nhưng đôi khi bạn cần ghi thẳng vào stream mà không thêm dấu xuống dòng hay định dạng thừa. Cả năm ngôn ngữ đều cho phép ghi trực tiếp vào stdout/stderr như một stream/writer: Go dùng fmt.Fprint, Rust dùng trait io::Write, Swift dùng FileHandle, còn Java ghi thẳng byte qua System.out/System.err (đều là PrintStream).
Ghi ra 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() với byte string thay vì write!(...,"…\n") — tránh cùng lúc
// hai clippy lint write_with_newline và explicit_write
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 worldGhi ra 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 errorTham khảo: github.com/miguelmota/golang-for-nodejs-developers#stdout