DEV Community

Cover image for Operators in GO
ZammaCode
ZammaCode

Posted on

Operators in GO

  • 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

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

  • Following is the output of above example.

Image description

  • 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
  • Create 03-02 AssignmentOperators folder in src folder and add a file assignment_op.go and type the following example.

  • Following is the output of above example.
    Image description

  • 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

  • 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

  • Following is the output.
    Image description

  • 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

  • Let’s create a folder 03-04 LogicalOperators in src folder of your workspace and then create file logical_op.go

  • On execution the following output will be shown.
    Image description

  • 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
  • To demonstrate the usage of bitwise operators create folder 03-05 BitwiseOperators and file bitwise_op.go
  • Following is the output.
    Image description

  • 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

Top comments (0)