gpt4 book ai didi

dictionary - Go 中的哪些语义规则决定何时发生单值赋值与何时发生二值赋值?

转载 作者:行者123 更新时间:2023-12-01 22:30:41 25 4
gpt4 key购买 nike

学习中map来自 A Tour of Go: Mutating Maps ,我发现令人惊讶的一件事是,我们可以使用单值赋值或双值赋值来访问映射中键的值。示例代码:

package main

import (
"fmt"
)

func main() {
m := map[int]int{2: 4, 3: 9, 4: 16}

// Example 1
fmt.Println(m[2])

// Example 2
v := m[2]
fmt.Println(v)

// Example 3
v, ok := m[2]
fmt.Println(v, ok)
}
输出:
4
4
4 true
使用相同语法同时支持单值和双值赋值涉及哪些语义规则? Go 中是否还有其他这种特殊形式,根据赋值运算符的左侧,以相同的语法同时支持单值和双值赋值?
此外,我可以写一个函数 foo()我自己可以根据赋值运算符的左侧返回一个值还是两个值?

最佳答案

映射索引操作的一两个值分配是 特殊表格 为方便起见,遗憾的是不能在“正常”作业中完成。
普通赋值表达式:
规范有以下关于元组 assignments 的内容:

A tuple assignment assigns the individual elements of a multi-valuedoperation to a list of variables. There are two forms. In the first,the right hand operand is a single multi-valued expression such as afunction call, a channel or map operation, or a type assertion. Thenumber of operands on the left hand side must match the number ofvalues. For instance, if f is a function returning two values,

x, y = f()

assigns the first value to x and the second to y. In the second form,the number of operands on the left must equal the number ofexpressions on the right, each of which must be single-valued, and thenth expression on the right is assigned to the nth operand on theleft:

one, two, three = '一', '二', '三'


这不会为赋值中的值的数量留下任何歧义的余地。
一值或二值表达式:
有 4 种情况,表达式左侧同时允许一个值和两个值。其中三个是 特殊表格 赋值表达式中,最后一个是 range条款。
索引表达式 :
Index expressions被定义为 a[x] 的形式, map 除外:

An index expression on a map a of type map[K]V used in an assignmentor initialization of the special form

v, ok = a[x]
v, ok := a[x]
var v, ok = a[x]

yields an additional untyped boolean value.


接收运营商 :
Receive Operator 也是如此。通常是 x <-ch 的形式:

A receive expression used in an assignment or initialization of thespecial form

x, ok = <-ch
x, ok := <-ch
var x, ok = <-ch
var x, ok T = <-ch

yieldsan additional untyped boolean result reporting whether thecommunication succeeded.


类型断言 :
再次提到 特殊表格 type assertions , 通常形式为 x.(T) :

A type assertion used in an assignment or initialization of thespecial form

v, ok = x.(T)
v, ok := x.(T)
var v, ok = x.(T)
var v, ok T1 = x.(T)

yields an additional untyped boolean value.


范围子句 :
for statement with range clause与它相关的语言更宽松,因为它不是对正常赋值表达式的修改:

Function calls on the left are evaluated once per iteration. For eachiteration, iteration values are produced as follows if the respectiveiteration variables are present:

Range expression                          1st value          2nd value

array or slice a [n]E, *[n]E, or []E index i int a[i] E
string s string type index i int see below rune
map m map[K]V key k K m[k] V
channel c chan E, <-chan E element e E

非赋值的用途:
如上所述,所有三个 特殊表格 仅用于作业。尝试在其他表达式(函数调用、返回等)中使用多值返回将失败,因为这些不是赋值并且不会从 中受益。特殊表格 .

关于dictionary - Go 中的哪些语义规则决定何时发生单值赋值与何时发生二值赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63508846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com