Cơ bản
In ra output
console.log/console.error trong Node.js so với fmt.Println/Printf (Go), println!/eprint! (Rust), print (Swift) và IO.println (Java 25).
- Phiên bản tối thiểu
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.19Swift ≥ 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 in ra stdout, console.error in ra stderr. Go tách các hàm rõ ràng hơn: fmt.Println in kèm xuống dòng, fmt.Printf nhận format string kiểu C, còn fmt.Fprintf ghi vào bất kỳ io.Writer nào — kể cả os.Stderr. Rust có macro tương đương (println!/eprint!) ngay trong ngôn ngữ; Swift chỉ có print đơn giản nên cần Foundation cho định dạng kiểu C và ghi ra stderr; Java 25 thêm IO.println cho compact source file, còn System.err vẫn dùng như trước.
In ra stdout và stderr
console.log('print to stdout')
console.log('format %s %d', 'example', 1)
console.error('print to stderr')package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("print to stdout")
fmt.Printf("format %s %v\n", "example", 1)
fmt.Fprintf(os.Stderr, "print to stderr")
}fn main() {
println!("print to stdout");
println!("format {} {}", "example", 1);
eprint!("print to stderr");
}import Foundation
print("print to stdout")
print(String(format: "format \("example") %d", 1))
FileHandle.standardError.write(Data("print to stderr".utf8))void main() {
IO.println("print to stdout");
IO.println("format %s %d".formatted("example", 1));
System.err.print("print to stderr");
}print to stdout
format example 1
print to stderrTham khảo: github.com/miguelmota/golang-for-nodejs-developers#printing