Operators in Go are tools for performing operations on variables and values. For example, the +
operator can be used to add two values together.
Example:
Go
x
package main
import ("fmt")
func main() {
var total = 11 + 8
fmt.Println(total)
}
Output:
Go
19
The +
operator can also add a variable with a value or another variable:
Example:
Go
package main
import ("fmt")
func main() {
var (
horsepower1 = 100 + 50 // 150
horsepower2 = horsepower1 + 250 // 400
totalHorsepower = horsepower2 + horsepower2 // 800
)
fmt.Println(totalHorsepower)
}
Output:
Go
800
Types of Go Operators
Go divides its operators into the following categories:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators