Go operators

Staying true to its simplistic nature, operators in Go do exactly what you would expect, mainly, they allow operands to be combined into expressions. There are no hidden surprise behaviors with Go operators as there is no support for operator-overloading as found in C++ or Scala. This was a deliberate decision from the designers to keep the semantics of the language simple and predictable.

This section explores the most common operators that you will encounter as you start with Go. Other operators are covered throughout other chapters of the book.

Arithmetic operators

The following table summarizes the arithmetic operators supported in Go.

Operator

Operation

Compatible types

*, /, -

Multiplication, division, and subtraction

Integers, floating points, and complex numbers

%

Remainder

Integers

+

Addition

Integers, floating points, complex numbers, and strings (concatenation)

Note that the addition operator, +, can be applied to strings such as in the expression var i = "G is" + " for Go". The two string operands are concatenated to create a new string that is assigned to variable i.

The increment and decrement operators

As with other C-like languages, Go supports the ++ (increment) and the -- (decrement) operators. When applied, these operators increase, or decrease, the operand's value by one, respectively. The following shows a function that uses the decrement operator to traverse the letters in string s in the reverse order:

func reverse(s string) { 
  for i := len(s) - 1; i >= 0; { 
    fmt.Print(string(s[i])) 
    i-- 
  } 
} 

It is important to note that the increment and decrement operators are statements, not expressions, as shown in the following snippets:

nextChar := i++       // syntax error 
fmt.Println("Current char", i--)   // syntax error 
nextChar++        // OK 

In the preceding examples, it is worth noting that the increment and decrement statements only support the postfix notation. The following snippet would not compile because of statement -i:

for i := len(s) - 1; i >= 0; { 
  fmt.Print(string(s[i])) 
  --i   //syntax error 
} 

Go assignment operators

Operator

Description

=

The simple assignment works as expected. It updates the left operand with the value of the right.

:=

The colon-equal operator declares a new variable, the left-side operator, and assigns it the value (and type) of the operand on the right.

+=

, -=

, *=

, /=

, %=

Apply the indicated operation using the left and the right operator and store the result in the left operator. For instance, a *= 8 implies a = a * 8.

Bitwise operators

Go includes full support for manipulating values at their most elemental forms. The following summarizes bitwise operators supported by Go:

Operator

Description

&

Bitwise AND

|

Bitwise OR

a ^ b

Bitwise XOR

&^

Bitwise AND NOT

^a

Unary bitwise complement

<<

Left-shift

>>

Right-shift

The right operand, in a shift operation, must be an unsigned integer or be able to be converted to an unsigned value. When the left operand is an untyped constant value, the compiler must be able to derive a signed integer type from its value or it will fail compilation.

The shift operators in Go also support both arithmetic and logical shifts. If the left operand is unsigned, Go automatically applies logical shift, whereas if it is signed, Go will apply an arithmetic shift.

Logical Operators

The following is a list of Go logical operations on Boolean values:

Operator

Operation

&&

Logical AND

||

Logical OR

!

Logical NOT

Comparison operators

All Go types can be tested for equality, including basic and composite types. However, only string, integer, and floating-point values can be compared using ordering operators, as is summarized in the following table:

Operator

Operation

Supported type

==

Equal

String, numeric, Boolean, interface, pointer, and struct types

!=

Not Equal

String, numeric, Boolean, interface, pointer, and struct types

<

, <=

, >

, >=

Ordering operators

String, integers, and floating points

Operator precedence

Since Go has fewer operators than are found in its counterparts such as C or Java, its operator precedence rules are far simpler. The following table lists Go's operator precedence echelon, starting with the highest:

Operation

Precedence

Multiplicative

*, /, %, <<, >>, &, &^

Additive

+, -, |, ^

Comparative

==, !=, <, <=, >, >=

Logical AND

&&

Logical OR

||

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.181