Operators in GO
Tags: go, googlecloud
Published: 2022-03-21 05:06:12

- Operators are used to perform operations on variables and values.
- Operators allow operands to be combined into expressions
- Operators are either binary operators or unary operators. A binary operator operation takes two operands and a unary operator operation takes only one operand.

## Arithmetic Operators
- Go supports the following arithmetic operators.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ulra3zecwtzhhgd8qcc.png)

- To demonstrate the use of arithmetic operators; create new folder as 03-01 ArithmeticOperators in src folder of the workspace and create arithmetic_op.go file and type the following code.
{% embed https://gist.github.com/ZammaCode/21c7ee0e394b411d67ec5b4b11ae541c.js %}

- Following is the output of above example.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67hnt62xtzzm11vob77p.png)
- Commit and push changes to GitHub.
```$ git add .
   $ git commit -m "03-01 ArithmeticOperators"
   $ git push origin master```

## Assignment Operators
- Assignment operators are used to assign values to variables.
- Following is the list of assignment operators.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1vkibmrpqshenzaljut.png)
- Create 03-02 AssignmentOperators folder in src folder and add a file assignment_op.go and type the following example.
 {% embed https://gist.github.com/ZammaCode/080074ca261e8086fe5bcc1aa21a729e.js %}

- Following is the output of above example.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pywukefvzi9e5mlyakc5.png)

- Commit changes to GitHub 
```$ git add .
$ git commit -m "03-02 AssignmentOperators"
$ git push origin master```

## Comparison/Relational Operators
- Comparison operators are used to compare two values.
- Following is the list of comparison operators.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sw6wbstqxg9wau4rn3hn.png)

- The following example demonstrates the usage of comparison operators. Create folder 03-03 ComparisonOperators in src folder and file with the name comparison_op.go 
{% embed https://gist.github.com/ZammaCode/b1bf4c6fc78dadddb15fbfbc30d365bf.js %}

- Following is the output.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odyxzhr6oh55hk3umyap.png)

- Commit changes to GitHub. 
```$ git add .
$ git commit -m "03-03 ComparisonOperators"
$ git push origin master```

## Logical Operators
- Logical operators are used for operations on Boolean values within the logical expressions.
- Following is the list of logical operators supported by Go.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qoqds1cb581s8ghj0gti.png)

- Let’s create a folder 03-04 LogicalOperators in src folder of your workspace and then create file logical_op.go 
{% embed https://gist.github.com/ZammaCode/f836abeb436e7d44378a78ad8372b8fe.js %}

- On execution the following output will be shown.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4o63nygmjf2i16td11yl.png)

- Commit changes to GitHub. 
```$ git add .
$ git commit -m "03-04 LogicalOperators"
$ git push origin master```

## Bitwise Operators
- Bitwise operators work on bits and perform bit-by-bit operation.
- Following is the list of bitwise operators supported by Go.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v29uhzqsiufoq1judhbm.png)
- To demonstrate the usage of bitwise operators create folder 03-05 BitwiseOperators and file bitwise_op.go
{% embed https://gist.github.com/ZammaCode/cfae321810f66e023908a688353b42d8.js %}
- Following is the output.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojhr414uti6h5ucijfo3.png)

- Finally commit Changes to GitHub.
```$ git add .
$ git commit -m "03-05 BitwiseOperators"
$ git push origin master```

## Operators Precedence
- Operator precedence determines the grouping of terms in an expression. 
- This affects how an expression is evaluated.
- The following table lists Go's operator precedence level, starting with the highest. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3eyxhsvj5bztuxr8ptv.png)