Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Go lang - Get Started
  • Go lang Overview
  • Install Go
  • Go Syntax
  • Create Go Program
  • Go DataTypes
  • Go Variables
  • Go Operators
  • Go if-else Statement
  • Go Switch Statement
  • Go For Loop
  • Working with Functions
  • Go Arrays
  • Go Slice
  • Go Struct
  • Go Map
  • Go Pointers
  • Go Packages
  • Go Modules
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Go Operators

Operators are symbols that allow us to perform various mathematical and logical operations on values and variables.

In Go, operators can be classified as below:

  • Arithmetic
  • Comparison
  • Logical
  • Bitwise
  • Assignment

Arithmetic Operators

The arithmetic operators can be used in the basic arithmetic operations on literal values or variables.

OperatorDescriptionSyntax
+Addition. Adds two valuesx + y
-Subtraction. Subtracts the second operand from the firstx – y
*Multiplyx * y
/Dividex / y
%Modulus. Gives the reminder after divisionx % y
++Increment operator. Increments the value by 1x ++
--Decrement operator. Decreases the value by 1x --

The following example demonstrates arithmetic operations such as addition, subtraction, and modulus.

Example: Arithmetic Operators
package main
import "fmt"

func main() {
		x := 5
	y := 4

    // Add x and y variables
    resultAdd := x + y
    fmt.Println(resultAdd) //output: 9

    //Subtract y from x
    resultSub := x - y
    fmt.Println(resultSub) // output: 1

    //Modulus of x and y
    resultMod := x % y
    fmt.Println(resultMod) // output: 1
}
Try it

Comparison Operators

Comparison operators are used to compare two literal values or variables.

OperatorDescriptionSyntax
==Equalx == y
!=Not Equalx != y
<Less thanx < y
<=Less than or equalx <= y
>Greater thanx > y
>=Greater than or equalx >= y

The following example demonstrates comparisons operators.

Example: Comparisons operators
package main
import "fmt"

func main() {
	x := 5
	y := 4

    fmt.Println (x == y) // output: false
    fmt.Println(x != y) // output: true
    fmt.Println (x &lt; y) // output: false
    fmt.Println (x <= y) // output: false
    fmt.Println (x > y) // output: true
    fmt.Println (x &gt;= y) // output: true
}
Try it

Logical Operators:

The logical operators are used to perform logical operations by combining two or more conditions. They return either true or false depending upon the conditions.

OperatorDescriptionSyntax
&&Logical AND. Returns true if both expressions are trueexpression1 && expression2
||Logical OR. Returns true if any one of the expressions is true.exp1 || exp2
!Logical NOT. Returns true if an expression is false. Returns false if the expression is true.! exp

The following example demonstrates the logical operators.

Example: Logical Operators
package main
import "fmt"

func main() {
	x ,y, z  := 5, 10, 20

	fmt.Println (x < y && x > z) //output: false
	fmt.Println(x &lt; y || x < z) //output: true
}
Try it

Bitwise Operators

The bitwise operators work on bits and perform bit-by-bit operation.

OperatorDescription
&bitwise AND
|bitwise OR
^bitwise XOR
<<left shift
>>Right shift

The following example demonstrates the bitwise operations.

Example: Bitwise Operators
package main
import "fmt"

func main() {

	var x, y = 3, 5
	var z int

	//Bitwise AND
	z = x & y
	fmt.Println("Bitwise AND ", z) // output: 1
	
    //Bitwise OR
	z = x | y
	fmt.Println("Bitwise OR ", z) //output: 7
	
    //Bitwise XOR
	z = x ^ y
	fmt.Println("Bitwise XOR ", z) //output: 6
}
Try it

In the above example, two int variables are x = 3 and y = 5. Binary of 3 is 0011 and 5 is 0101. so, x & y is 0001, which is numeric 1.x | y is 0111 which is numeric 7, and x ^ y is 0110 which is numeric 6.

Assignment Operators

The assignment operators are used to assign literal values or assign values by performing some arithmetic operation using arithmetic operators.

OperatorDescriptionSyntax
=Assign valuex = y
+=Add and assignx += y same as x = x + y
-=Subtract and assignx -= y same as x = x - y
*=Multiply and assignx *= y same as x = x * y
/=Divide and assignx /= y same as x = x/y
%=Divide and assign modulusx %= y same as x = x%y

The following example demonstrates the assignment operators.

Example: Assignment Operators
package main
import "fmt"

func main() {
	x, y := 10, 20

	//Assign
	x = y
	fmt.Println(" = ", x) //output: 20

	// Add and assign
	x = 15
	x += y
	fmt.Println("+= ", x) //output: 35

	// Subtract and assign
	x = 25
	x -= y
	fmt.Println("-=", x) //output: 5

	//Multiply and assign
	x = 2
	x *= y
	fmt.Println("*=", x) //output: 40

	//Divide and assign
	x = 100
	x /= y
	fmt.Println("/=", x) // output: 5

	//Divide and assign modulus
	x = 30
	x %= y
	fmt.Println(" %= ", x) //output: 10
}
Try it
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

contact@tutorialsteacher.com

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.