Go Operator Examples

Operators are the foundation of any programming language, enabling developers to perform different operations on variables and values. Go language offers a variety of operators for arithmetic, comparison, logical, and more. This blog post will illustrate some common operators in Go through examples.

Operators in Go are special symbols that tell the compiler to perform specific mathematical, logical, or relational operations and produce a final result. For example, arithmetic operators include `+` (addition), `-` (subtraction), `*` (multiplication), `/` (division), and `%` (modulus).

1. Go - sign operators

The + and - signs indicate the sign of a value. The plus sign can be used to signal that we have a positive number.

package main

import "fmt"

func main() {

    fmt.Println(2)
    fmt.Println(+2)
    fmt.Println(-2)
}

Output:

2
2
-2

2. Go - assignment operator

The assignment operator = assigns a value to a variable. A variable is a placeholder for a value.

package main

import "fmt"

func main() {
    var x = 2
    fmt.Println(x)
}

Output:

2
-2

Go has a short variable declaration operator :=; it declares a variable and assigns a value in one step. The x := 2 is equal to var x = 2.

package main

import "fmt"

func main() {
    x := 2
    fmt.Println(x)
}

Output:

2
-2

3. Go - increment and decrement operators

We often increment or decrement a value by one in programming. Go has two convenient operators for this: ++ and --.

package main

import "fmt"

func main() {

    x := 5

    x++
    x++

    fmt.Println(x)

    x--
    fmt.Println(x)
}

Output:

7
6

4. Go - arithmetic operators

The following example shows arithmetic operations.

package main

import "fmt"

func main() {

    var a = 2
    var b = 3
    var c = 4

    var add = a + b + c
    var sb = c - a
    var mult = a * b
    var div = c / 2
    var rem = c % a

    fmt.Println(add)
    fmt.Println(sb)
    fmt.Println(mult)
    fmt.Println(div)
    fmt.Println(rem)
}

Output:

9
2
6
2
0

In the above example, we use addition, subtraction, multiplication, division, and remainder operations.

5. Go - Boolean operators

Boolean operators are also called logical.

Many expressions result in a boolean value. For instance, boolean values are used in conditional statements.

package main

import "fmt"

func main() {

    var x = 3
    var y = 5

    fmt.Println(x == y)
    fmt.Println(y > x)

    if y > x {

        fmt.Println("y is greater than x")
    }
}

Output:

false
true
y is greater than x

Go - Logical and (&&) operator

The code example shows the logical and (&&) operators. It evaluates to true only if both operands are true.

package main

import "fmt"

func main() {

    var a = true && true
    var b = true && false
    var c = false && true
    var d = false && false

    fmt.Println(a)
    fmt.Println(b)
    fmt.Println(c)
    fmt.Println(d)
}

Output:

true
false
false
false

Go - Logical or (||) operator

The logical or (||) operator evaluates to true if either of the operands is true.

package main

import "fmt"

func main() {

    var a = true || true
    var b = true || false
    var c = false || true
    var d = false || false

    fmt.Println(a)
    fmt.Println(b)
    fmt.Println(c)
    fmt.Println(d)
}

Output:

true
true
true
false

Go - negation operator !

The negation operator ! makes true false and false true.

package main

import "fmt"

func main() {

    fmt.Println(!true)
    fmt.Println(!false)
}

Output:

false
true
true

6. More Operator Examples

package main

import "fmt"

func main() {
	// Step 1: Arithmetic operators
	sum := 10 + 5          // Addition
	difference := 10 - 5   // Subtraction
	product := 10 * 5      // Multiplication
	quotient := 10 / 5     // Division
	remainder := 10 % 5    // Modulus

	// Step 2: Comparison operators
	isEqual := 10 == 5     // Equality
	isNotEqual := 10 != 5  // Inequality
	isGreater := 10 > 5    // Greater than
	isLess := 10 < 5       // Less than

	// Step 3: Logical operators
	andCondition := (10 > 5) && (5 == 5) // Logical AND
	orCondition := (10 > 5) || (5 == 10) // Logical OR
	notCondition := !(10 == 5)           // Logical NOT

	// Step 4: Combined expression using multiple operators
	combinedResult := (10+5)*(10-5) == 75 && 10%5 == 0

	// Printing the results
	fmt.Println("Sum:", sum)
	fmt.Println("Difference:", difference)
	fmt.Println("Product:", product)
	fmt.Println("Quotient:", quotient)
	fmt.Println("Remainder:", remainder)
	fmt.Println("isEqual:", isEqual)
	fmt.Println("isNotEqual:", isNotEqual)
	fmt.Println("isGreater:", isGreater)
	fmt.Println("isLess:", isLess)
	fmt.Println("andCondition:", andCondition)
	fmt.Println("orCondition:", orCondition)
	fmt.Println("notCondition:", notCondition)
	fmt.Println("combinedResult:", combinedResult)
}

Output:

Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
isEqual: false
isNotEqual: true
isGreater: true
isLess: false
andCondition: true
orCondition: true
notCondition: true
combinedResult: true

Explanation:

1. package main - Defines the package name for the Go program.

2. import "fmt" - Imports the Format package, necessary for printing formatted output.

3. Arithmetic operator examples assign the results of arithmetic operations to variables.

4. Comparison operators test for equality, inequality, greater, and less than conditions.

5. Logical operators combine boolean expressions with AND, OR, and NOT operations.

6. A combined expression is used to show a more complex use case involving arithmetic and logical operators.

7. fmt.Println statements print the result of each operation to the console.

8. The output shows the results of various operations, with the arithmetic operations resulting in numerical values, the comparison operations in boolean values, and the logical and combined expressions also yielding boolean values. Each operation's outcome reflects the nature of the operator used.


Comments