Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
Operators in Go (often referred to as Golang) are special symbols that perform operations on variables and values. These operators allow you to manipulate data and make decisions based on it. Here's a detailed tutorial on operators in Go:
These are used for mathematical operations:
Used for comparing two values:
Used to combine conditional statements:
Used to assign values:
Operate at the bit level:
Let's see a few examples for a better understanding:
package main import "fmt" func main() { a := 10 b := 20 fmt.Println("a + b =", a + b) // 30 fmt.Println("a < b", a < b) // true fmt.Println("a & b", a & b) // 0 (in binary: 1010 & 10100 = 00000) fmt.Println("a | b", a | b) // 30 (in binary: 1010 | 10100 = 11110) p := &a fmt.Println("Address of a:", p) // Memory address of variable a fmt.Println("Value using pointer:", *p) // 10 }
Operators are fundamental building blocks in Golang, allowing for a wide range of operations, from basic arithmetic to complex logical and bitwise manipulations. Familiarity with these operators is key to writing efficient and effective Go programs.
Arithmetic Operators in Golang:
package main import "fmt" func main() { a, b := 5, 2 fmt.Println("Addition:", a+b) // Output: 7 fmt.Println("Subtraction:", a-b) // Output: 3 fmt.Println("Multiplication:", a*b) // Output: 10 fmt.Println("Division:", a/b) // Output: 2 fmt.Println("Modulus:", a%b) // Output: 1 }
Relational Operators in Golang:
package main import "fmt" func main() { a, b := 5, 2 fmt.Println("Equal:", a == b) // Output: false fmt.Println("Not Equal:", a != b) // Output: true fmt.Println("Greater Than:", a > b) // Output: true fmt.Println("Less Than:", a < b) // Output: false fmt.Println("Greater or Equal:", a >= b) // Output: true fmt.Println("Less or Equal:", a <= b) // Output: false }
Logical Operators in Golang:
package main import "fmt" func main() { x, y := true, false fmt.Println("AND:", x && y) // Output: false fmt.Println("OR:", x || y) // Output: true fmt.Println("NOT:", !x) // Output: false }
Bitwise Operators in Golang:
package main import "fmt" func main() { a, b := 5, 3 fmt.Println("AND:", a&b) // Output: 1 fmt.Println("OR:", a|b) // Output: 7 fmt.Println("XOR:", a^b) // Output: 6 fmt.Println("Shift Left:", a<<1) // Output: 10 fmt.Println("Shift Right:", a>>1) // Output: 2 }
Assignment Operators in Golang:
package main import "fmt" func main() { a := 5 a += 2 // Equivalent to a = a + 2 fmt.Println("Value of a:", a) // Output: 7 a -= 3 // Equivalent to a = a - 3 fmt.Println("Value of a:", a) // Output: 4 a *= 2 // Equivalent to a = a * 2 fmt.Println("Value of a:", a) // Output: 8 a /= 4 // Equivalent to a = a / 4 fmt.Println("Value of a:", a) // Output: 2 }
Comparison Operators in Golang:
package main import "fmt" func main() { a, b := 5, 2 fmt.Println("Equal:", a == b) // Output: false fmt.Println("Not Equal:", a != b) // Output: true }
Unary Operators in Golang:
package main import "fmt" func main() { a := 5 fmt.Println("Unary Plus:", +a) // Output: 5 fmt.Println("Unary Minus:", -a) // Output: -5 fmt.Println("Logical NOT:", !true) // Output: false }
Overloaded Operators in Golang:
// Operator overloading is not supported in Golang.