Go PART 3: OPERATORS AND EXPRESSIONS IN Go
Please Subscribe Youtube| Like Facebook | Follow Twitter
Operators and Expressions in Go
In this article, we’ll cover the most commonly used operators and expressions in Go, along with their syntax, examples, and outputs.
Go is a statically typed programming language that supports a wide range of operators and expressions. These operators and expressions allow you to perform various operations on variables, constants, and literals. In this article, we will explore the different types of operators and expressions in Go, and we’ll provide examples to demonstrate their usage.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numerical values. The following table shows the arithmetic operators in Go:
Operator | Description | Example |
---|---|---|
+ | Addition. Adds two operands. | x + y |
– | Subtraction. Subtracts the right-hand operand from the left-hand operand. | x – y |
* | Multiplication. Multiplies two operands. | x * y |
/ | Division. Divides the left-hand operand by the right-hand operand. | x / y |
% | Modulus. Returns the remainder of the division of the left-hand operand by the right-hand operand. | x % y |
Let’s take a look at some examples of arithmetic operators in Go:
package main
import "fmt"
func main() {
num1 := 10
num2 := 3
// Addition
result := num1 + num2
fmt.Printf("Addition: %d\n", result)
// Subtraction
result = num1 - num2
fmt.Printf("Subtraction: %d\n", result)
// Multiplication
result = num1 * num2
fmt.Printf("Multiplication: %d\n", result)
// Division
result = num1 / num2
fmt.Printf("Division: %d\n", result)
// Modulus
result = num1 % num2
fmt.Printf("Modulus: %d\n", result)
}
Output
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3 Modulus: 1
Comparison Operators
Comparison operators are used to compare values. The following table shows the comparison operators in Go:
Operator | Description | Example |
---|---|---|
== | Equal to. Returns true if the left-hand operand is equal to the right-hand operand. | x == y |
!= | Not equal to. Returns true if the left-hand operand is not equal to the right-hand operand. | x != y |
> | Greater than. Returns true if the left-hand operand is greater than the right-hand operand. | x > y |
< | Less than. Returns true if the left-hand operand is less than the right-hand operand. | x < y |
>= | Greater than or equal to. Returns true if the left-hand operand is greater than or equal to the right-hand operand. | x >= y |
<= | Less than or equal to. Returns true if the left-hand operand is less than or equal to the right-hand operand. | x <= y |
Here’s an example Go program that uses the comparison operators:
package main
import "fmt"
func main() {
num1 := 5
num2 := 3
fmt.Printf("Is %d equal to %d? %t\n", num1, num2, num1 == num2)
fmt.Printf("Is %d not equal to %d? %t\n", num1, num2, num1 != num2)
fmt.Printf("Is %d greater than %d? %t\n", num1, num2, num1 > num2)
fmt.Printf("Is %d less than %d? %t\n", num1, num2, num1 < num2)
fmt.Printf("Is %d greater than or equal to %d? %t\n", num1, num2, num1 >= num2)
fmt.Printf("Is %d less than or equal to %d? %t\n", num1, num2, num1 <= num2)
}
Output
Is 5 equal to 3? false Is 5 not equal to 3? true Is 5 greater than 3? true Is 5 less than 3? false Is 5 greater than or equal to 3? true Is 5 less than or equal to 3? false
Logical Operators
Logical operators are used to combine multiple expressions and evaluate them as a single Boolean expression. The following table shows the Logical operators in Go:
Operator | Description | Example |
---|---|---|
&& | Logical AND operator. Returns true only if both operands are true. | true && false // false |
|| | Logical OR operator. Returns true if at least one operand is true. | true || false // true |
! | Logical NOT operator. Reverses the logical state of an operand. | !true // false, !false // true |
Here’s an example program to demonstrate the logical operators:
package main
import "fmt"
func main() {
x := 10
y := 5
z := 8
// logical AND operator
if x > y && x < z {
fmt.Println("x is between y and z")
} else {
fmt.Println("x is not between y and z")
}
// logical OR operator
if x < y || x > z {
fmt.Println("x is either less than y or greater than z")
} else {
fmt.Println("x is between y and z")
}
// logical NOT operator
if !(x == y) {
fmt.Println("x is not equal to y")
} else {
fmt.Println("x is equal to y")
}
}
Output
x is not between y and z x is either less than y or greater than z x is not equal to y
Conclusion
In conclusion, operators and expressions are an essential part of any programming language, including Go. Operators are symbols or keywords that perform an action on one or more operands to produce a result. There are several types of operators in Go, including arithmetic, comparison, and logical operators, each with its own set of rules and precedence.
Go Beginner Tutorial Series
- Go Part 1: Setup and Introduction
- GO PART 2: UNDERSTANDING BASIC DATA TYPES AND VARIABLES IN GO
- Go PART 3: OPERATORS AND EXPRESSIONS IN Go
- GO PART 4: CONTROL FLOW STATEMENTS IN GO
- Go PART 5: Functions In Go
- GO PART 6: ARRAYS AND SLICES IN GO
- Go Part 7: String Manipulation In Go
- Go Part 8: Struct Type in Go
- Go Part 9: Pointers in Go
- Go Part 10: Error Handling in Go