Functions
Destructuring
Object destructuring in Node.js compared to struct pattern destructuring in Rust, tuple destructuring in Swift, record patterns in Java, and multiple assignment in Go.
- Minimum versions
- Node.js ≥ 12.20Go ≥ 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.
Node.js destructures an object directly into multiple variables with { key, value } = obj. Rust has real struct destructuring through pattern matching (let Obj { key, value } = obj) — the closest match to JS. Swift doesn't destructure a struct by field name directly, but it does destructure tuples (let (key, value) = (...)). Java has had record patterns since version 21, letting you destructure a record right inside instanceof. Go has no destructuring syntax for structs, but you get the same result with multiple assignment or a function that returns multiple values.
Pulling multiple values out of an object
const obj = { key: 'foo', value: 'bar' };
const { key, value } = obj;
console.log(key, value); // foo barpackage main
import "fmt"
type Obj struct {
Key string
Value string
}
func (o *Obj) Read() (string, string) {
return o.Key, o.Value
}
func main() {
obj := Obj{
Key: "foo",
Value: "bar",
}
// option 1: multiple variable assignment
key, value := obj.Key, obj.Value
fmt.Println(key, value) // foo bar
// option 2: return multiple values from a function
key, value = obj.Read()
fmt.Println(key, value) // foo bar
}struct Obj {
key: String,
value: String,
}
fn main() {
let obj = Obj { key: "foo".to_string(), value: "bar".to_string() };
// real pattern destructuring, field names must match
let Obj { key, value } = obj;
println!("{key} {value}"); // foo bar
}struct Obj {
let key: String
let value: String
}
let obj = Obj(key: "foo", value: "bar")
// Swift doesn't destructure a struct by field name directly,
// but tuple destructuring works
let (key, value) = (obj.key, obj.value)
print(key, value) // foo barrecord Obj(String key, String value) {}
void main() {
Obj obj = new Obj("foo", "bar");
// record pattern (Java 21+): destructure right inside instanceof
if (obj instanceof Obj(String key, String value)) {
IO.println(key + " " + value); // foo bar
}
}Reference: github.com/miguelmota/golang-for-nodejs-developers#destructuring