
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Arithmetic Operators
Following table shows all the arithmetic operators supported by Go language. Assume variable A holds 10 and variable B holds 20, then:
Operator | Description | Example |
---|---|---|
+ | Adds two operands | A + B gives 30 |
- | Subtracts second operand from the first | A - B gives -10 |
* | Multiplies both operands | A * B gives 200 |
/ | Divides the numerator by the denominator. | B / A gives 2 |
% | Modulus operator; gives the remainder after an integer division. | B % A gives 0 |
++ | Increment operator. It increases the integer value by one. | A++ gives 11 |
-- | Decrement operator. It decreases the integer value by one. | A-- gives 9 |
Example
Try the following example to understand all the arithmetic operators available in Go programming language −
package main import "fmt" func main() { var a int = 21 var b int = 10 var c int c = a + b fmt.Printf("Line 1 - Value of c is %d\n", c ) c = a - b fmt.Printf("Line 2 - Value of c is %d\n", c ) c = a * b fmt.Printf("Line 3 - Value of c is %d\n", c ) c = a / b fmt.Printf("Line 4 - Value of c is %d\n", c ) c = a % b fmt.Printf("Line 5 - Value of c is %d\n", c ) a++ fmt.Printf("Line 6 - Value of a is %d\n", a ) a-- fmt.Printf("Line 7 - Value of a is %d\n", a ) }
When you compile and execute the above program, it produces the following result −
Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of a is 22 Line 7 - Value of a is 21
Advertisements