Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between := and = operators in Go

Tags:

go

What is the difference between the = and := operators, and what are the use cases for them? They both seem to be for an assignment?

like image 239
Chris Avatar asked Jul 26 '13 21:07

Chris


People also ask

What is the := operator in Go?

Go has a short variable declaration operator := ; it declares a variable and assigns a value in one step.

What is the difference between := and assignment operators?

= 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.

What does += operator mean in Golang?

+= 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.


1 Answers

In Go, := is for declaration + assignment, whereas = is for assignment only.

For example, var foo int = 10 is the same as foo := 10.

like image 104
Chaos Avatar answered Oct 02 '22 16:10

Chaos