destructuring
Node.js
const obj = { key: 'foo', value: 'bar' }
const { key, value } = obj
console.log(key, value)
Output
foo bar
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)
}
Output
foo bar
foo bar
当前内容版权归 miguelmota 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 miguelmota .