Go Operators
Go supports a number of different operators. These are symbols that modify the value of one or more expressions. These symbols include arithmetic operators, comparison operators, logical operators, bitwise operators, and assignment operators.
Arithmetic Operators
Arithmetic operators take one or two numeric expressions and return a numeric result.
+Takes two expressions,(A + B), and returns the sum of the two expressions.-Takes two expressions,(A - B), and returns the difference of the two expressions, the first expression minus the second.*Takes two expressions,(A * B), and returns the product of the two expressions./Takes two expressions,(A / B), and returns the first expression divided by the second.%Takes two expressions,(A % B), and returns the modulus, the remainder after the integer division of the first expression by the second.++Takes one expression,(A++), and returns the expression incremented by one.--Takes one expression,(B--), and returns the expression decremented by one.
Comparison Operators
Comparison operators compare two expressions and return true or false based on the operator and the values of the expression. Go has the following comparison operators.
==: Takes two expressions,(A == B), and returnstrueif both expressions are equal.!=: Takes two expressions,(A != B), and returnstrueif both expressions are not equal.<: Takes two expressions,(A < B), and returnstrueif the left expression is numerically less than or comes lexically before the right one.<=: Takes two expressions,(A <= B), and returnstrueif the left expression is numerically less than or comes lexically before the right one, or if both expressions are equal.>: Takes two expressions,(A > B), and returnstrueif the left expression is numerically greater than or comes lexically after the right one.>=: Takes two expressions,(A >= B), and returnstrueif the left expression is numerically greater than or comes lexically after the right one, or if both expressions are equal.
Logical Operators
Logical operators return a true or false value based on the true or false values of the expressions they operate on. When expressions are numeric, a zero value is treated as false and a non-zero value is treated as true.
&&AND operator: Takes two expressions,(A && B), and returnstrueif both expressions aretrue.||OR operator: Takes two expressions,(A || B), and returnstrueif one of them istrue.!NOT operator: Takes one expression,!(A), and returnstrueif its value isfalse.
Bitwise Operators
Bitwise operators manipulate the individual bits of their numeric expressions.
&Binary AND Operator: Takes two expressions,(A & B), result will have a bit set if the corresponding bit in each expression is set.|Binary OR Operator: Takes two expressions,(A | B), result will have a bit set if either expression has the corresponding bit set.^Binary XOR Operator: Takes two arguments,(A ^ B), result will have a bit set if only one corresponding bit is set between the two expressions.<<Binary Left Shift Operator: Takes two arguments,(A << B), result will be the first argument with its bits moved to the left the number of positions specified by the second.>>Binary Right Shift Operator: Takes two arguments,(A >> B), result will be the first argument with its bits moved to the right the number of positions specified by the second.
Assignment Operators
Assignment operators store the value of an expression into a variable. The variable being assigned to is always on the left.
=: Assigns the value of the expression to the right to the variable on the left.C = A + Bstores the value ofA + Binto the variableC.+=: Adds the value on the right to the value in the variable on the left and assigns it to the variable.C += Ais the same asC = C + A.-=: Subtracts the value on the right from the value in the variable on the left and assigns it to the variable.C -= Ais the same asC = C - A.*=: Multiplies the value on the right to the value in the variable to the left and assigns it to the variable.C *= Ais the same asC = C * A./=: Divides the value in the variable on the left by the value on the right and stores the result in the variable.C /= Ais the same asC = C / A.%=: Performs an integer division of the value in the variable on the left by the value on the right and stores the remainder in the variable.C %= Ais the same asC = C % A.<<=: Does a binary left shift operation on the value in the variable on the left by the number of positions specified on the right and stores the result in the variable.C <<= Ais the same asC = C << A.>>=: Does a binary right shift operation on the value in the variable on the left by the number of positions specified on the right and stores the result in the variable.C >>= Ais the same asC = C >> A.&=: Does a bitwise AND operation between the value in the variable on the left with the value on the right and stores the result in the variable.C &= Ais the same asC = C & A.|=: Does a bitwise OR operation between the value in the variable on the left and the value on the right and stores the result in the variable.C |= Ais the same asC = C | A.^=: Does a bitwise XOR operation between the value in the variable on the left and the value on the right and stores the value in the variable.C ^= Ais the same asC = C ^ A.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use Go (Golang), an open-source programming language supported by Google!
- Beginner Friendly.6 hours