What is the difference between the =
and :=
operators, and what are the use cases for them? They both seem to be for an assignment?
Go has a short variable declaration operator := ; it declares a variable and assigns a value in one step.
= operator assigns a value either as a part of the SET statement or as a part of the SET clause in an UPDATE statement, in any other case = operator is interpreted as a comparison operator. On the other hand, := operator assigns a value and it is never interpreted as a comparison operator.
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A. -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand.
In Go, :=
is for declaration + assignment, whereas =
is for assignment only.
For example, var foo int = 10
is the same as foo := 10
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With