Moment For Technology

Go (5) assignment statement | weekend study

Posted on Aug. 26, 2023, 4:24 a.m. by Kimberly Frazier
Category: The back-end Tag: The back-end

This article has participated in the weekend learning program, click the link to see more details:

Variable assignment

  1. 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
  2. The composition of a particular binary arithmetic operator and an assignment statement has a succinct formcount[x] *= scale
  3. Numeric variables can also support ++ increment and -- decrement statements (increment and decrement are statements, not expressions, so expressions like x = i++ are wrong)v++

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

Expression assignment

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.

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.