This article has participated in the weekend learning program, click the link to see more details: juejin.cn/post/696572...
- The simplest assignment statement is to place the variable to be assigned to the left of = and the expression for the new value to the right of =.
x = 1
- The composition of a particular binary arithmetic operator and an assignment statement has a succinct form
count[x] *= scale
- Numeric variables can also support ++ increment and -- decrement statements (increment and decrement are statements, not expressions, so expressions like x = i++ are wrong)
Yuan group assignment
Tuple assignment is another form of assignment statement that allows the values of multiple variables to be updated simultaneously. Before assignment, all expressions on the right side of the assignment statement are evaluated, and then the values of the corresponding variables on the left are uniformly updated. x, y = y, x
Some expressions produce multiple values, such as calling a function with multiple return values. When such a function call occurs in an expression to the right of a tuple assignment, the number of variables on the left must be the same as on the right. f, err = os.Open("foo.txt")
For example, map lookup, type assertion, or channel receive appearing to the right of an assignment statement may all produce two results, with an additional Boolean result indicating whether the operation was successful:
v, ok = m[key] // map lookup v, ok = x.(T) // type assertion v, ok = -ch // channel receive Copy the code
But it doesn't have to be two outcomes, it could be just one. In cases where a value produces a result, zero is returned when a map lookup fails, a runtime panic exception is sent when a type assertion fails, and zero is returned when a channel receive fails (blocking is not considered a failure).
Sex can be assigned a value
Whether an assignment is made implicitly or explicitly, the variable on the left of the assignment statement must have the same data type as the final evaluated value on the right. To put it more bluntly, assignment statements are allowed only if the value on the right is assignable to the variable on the left.