Golang Increment Operator

Golang Increment Operator takes a single operand (a number) and increments it by one.

Golang Increment Operator applies to integers, floats, and complex values.

In this tutorial, we will learn about the syntax to use Increment Operator, and some example scenarios on how to use increment operator.

Syntax

The syntax to increment the value of x, using increment operator is

</>
Copy
x++

where x is operand and ++ is the operator symbol.

Examples

Increment Integer

In the following example, we take an integer and increment it using increment operator.

Example.go

</>
Copy
package main

func main() {
	x := 5
	println("Before increment, x = ", x)

	x++
	println("After  increment, x = ", x)
}

Output

Before increment, x =  5
After  increment, x =  6

Increment Float

In the following example, we take a floating point number and increment it using increment operator.

Example.go

</>
Copy
package main

func main() {
	x := 3.14
	println("Before increment, x = ", x)

	x++
	println("After  increment, x = ", x)
}

Output

Before increment, x =  +3.140000e+000
After  increment, x =  +4.140000e+000

Increment Complex Number

In the following example, we take a complex number and increment it using increment operator. Only the real part of this complex number increments, but the imaginary part remains unchanged.

Example.go

</>
Copy
package main

func main() {
	x := complex(3, 7)
	println("Before increment, x = ", x)

	x++
	println("After  increment, x = ", x)
}

Output

Before increment, x =  (+3.000000e+000+7.000000e+000i)
After  increment, x =  (+4.000000e+000+7.000000e+000i)

Conclusion

In this Golang Tutorial, we learned about Increment operator in Go language, with the syntax and example programs.