destructuring
# Node.js
const obj = { key: "foo", value: "bar" };
const { key, value } = obj;
console.log(key, value);
1
2
3
4
2
3
4
Output
foo bar
1
# Go
package 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)
// option 2: return multiple values from a function
key, value = obj.Read()
fmt.Println(key, value)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Output
foo bar
foo bar
1
2
2
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22