Golang Decrement Operator
Golang Decrement Operator takes a single operand (a number) and decrements it by one.
Golang Decrement Operator applies to integers, floats, and complex values.
In this tutorial, we will learn about the syntax to use Decrement Operator, and some example scenarios on how to use decrement operator.
Syntax
The syntax to decrement the value of x, using decrement operator is
x--
where x
is operand and --
is the operator symbol.
Examples
Decrement Integer
In the following example, we take an integer and decrement it using decrement operator.
Example.go
package main
func main() {
x := 5
println("Before decrement, x = ", x)
x--
println("After decrement, x = ", x)
}
Output
Before decrement, x = 5
After decrement, x = 4
Decrement Float
In the following example, we take a floating point number and decrement it using decrement operator.
Example.go
package main
func main() {
x := 3.14
println("Before decrement, x = ", x)
x--
println("After decrement, x = ", x)
}
Output
Before decrement, x = +3.140000e+000
After decrement, x = +2.140000e+000
Decrement Complex Number
In the following example, we take a complex number and decrement it using decrement operator. Only the real part of this complex number decrements, but the imaginary part remains unchanged.
Example.go
package main
func main() {
x := complex(3, 7)
println("Before decrement, x = ", x)
x--
println("After decrement, x = ", x)
}
Output
Before decrement, x = (+3.000000e+000+7.000000e+000i)
After decrement, x = (+2.000000e+000+7.000000e+000i)
Conclusion
In this Golang Tutorial, we learned about Decrement operator in Go language, with the syntax and example programs.