An addition operator adds two variable or value provided to it. It returns the result after performing operation. If multiple operator exists in the operation, it evaluate the operation from left to right.
1 <variable> + <variable>
2 <value> + <value>
3
4 <variable> + <variable> + <value>
1 package main
2
3 import "fmt"
4
5 func main() {
6 var a, b int
7
8 a = 10
9 b = 20
10
11 c := a + b
12
13 fmt.Printf("Sum of a + b is : %d", c)
14 }
In the above example, we have define two integer variables and initialize this variable with 10, 20 respectively. A addition of two variables is assign to variable C and printed the result.
Multiple addition operator in same expression
Multiple addition operator in same expression
1 package main
2
3 import "fmt"
4
5 func main() {
6
7 a := 10
8 b := 20
9
10 c := a + b + 20
11
12 fmt.Printf("Sum of a + b + 20 is : %d", c)
13 }
In the above example, a variable declared and initialized with respective values. An expression is specified that perform addition of two variable and value that includes multiple addition operator.
If an expression includes multiple addition operator, an expression evaluated from left to right, hence, it perform addition of a + b and the resulting value added with value 20 and assigned to the variable. A resulting value will be printed using printf() function.
1 Sum of a + b + 20 is : 50
Note : If expression includes other operator, an expression evaluated based on operator precedence.
Related options for your search