Tuesday, December 29, 2020

Go Operators

Want to know more about operators in Go? Read to understand.

If you come from JavaScript, Java, C, C# or Python you will feel at home with Go operators. On this article we will provide a summary of the operators in Go and provide some examples.

What are operators?

But what are operators? Operators are constructs which behave generally like functions, but which differ syntactically or semantically. Common simple examples include arithmetic, comparison, and logical.

Which operators are supported in Go?

The following are the operators (including assignment operators) and punctuation supported in Go:

+    &     +=    &=     &&    ==    !=    (    )
-    |     -=    |=     ||    <     <=    [    ]
*    ^     *=    ^=     <-    >     >=    {    }
/    <<    /=    <<=    ++    =     :=    ,    ;
%    >>    %=    >>=    --    !     ...   .    :
     &^          &^=

Types of operators

Go operators are usually categorized based on functionality:

  • Arithmetic Operators
  • Comparison Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

Arithmetic operators

Arithmetic operators apply to numeric values and yield a result of the same type as the first operand. The four standard arithmetic operators (+, -, *, /) apply to integer, floating-point, and complex types; + also applies to strings. The bitwise logical and shift operators apply to integers only.

+    sum                    integers, floats, complex values, strings
-    difference             integers, floats, complex values
*    product                integers, floats, complex values
/    quotient               integers, floats, complex values
%    remainder              integers

&    bitwise AND            integers
|    bitwise OR             integers
^    bitwise XOR            integers
&^   bit clear (AND NOT)    integers

<<   left shift             integer << unsigned integer
>>   right shift            integer >> unsigned integer 

Comparison operators

Comparison operators compare two operands and yield an untyped boolean value. They are:

==    equal
!=    not equal
<     less
<=    less or equal
>     greater
>=    greater or equal

Logical operators

Logical operators apply to boolean values and yield a result of the same type as the operands. The right operand is evaluated conditionally.

&&    conditional AND    p && q  is  "if p then q else false"
||    conditional OR     p || q  is  "if p then true else q"
!     NOT                !p      is  "not p" 

Address operators

For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal. If the evaluation of x would cause a run-time panic, then the evaluation of &x does too.

For an operand x of pointer type *T, the pointer indirection *x denotes the variable of type T pointed to by x. If x is nil, an attempt to evaluate *x will cause a run-time panic.

&x
&a[f(2)]
&Point{2, 3}
*p
*pf(x)

var x *int = nil
*x   // causes a run-time panic
&*x  // causes a run-time panic 

Operator precedence

Unary operators

Unary operators have the highest precedence. As the ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy. As a consequence, statement *p++ is the same as (*p)++.

Binary operators

There are five precedence levels for binary operators. Multiplication operators bind strongest, followed by addition operators, comparison operators, && (logical AND), and finally || (logical OR).

Precedence    Operator
    5             *  /  %  <<  >>  &  &^
    4             +  -  |  ^
    3             ==  !=  <  <=  >  >=
    2             &&
    1             ||

Binary operators of the same precedence associate from left to right. For instance, x / y * z is the same as (x / y) * z.

+x
23 + 3*x[i]
x <= f()
^a >> b
f() || g()
x == y+1 && <-chanPtr > 0

Conclusion

On this article we discussed a little about Go operators. If you come from JavaScript, Java, C, C# or Python you will feel at home with Go operators. Knowing and understanding operators in any programming language is important for writing better and readable code, we recommend that you read the Go spec for more information.

Source

Any comment about this page? Please contact us on Twitter

Featured Article

Raw String Literals in Go

Learn what are Raw String Literals in Go and how to use them On a previous posts we discussed Runes, Variables and Strings in ...

Popular Posts