What are arithmetic operators in Golang?
About Golang
Golang, also known as Go, is an open-source, compiled, and statically-typed programming language developed by Google. The language has a simple syntax and robust standard libraries.
It is used in real-world applications and supports imperative and OOP paradigms.
Operators
An operator is a symbol or function that indicates an operation. In Go, arithmetic operators perform just as they do in mathematics. They can be used as a calculator in Go.
Arithmetic operators
Here are some common arithmetic operators:
-
x+y: This provides the sum of two variables,xandy. -
x-y: This provides the difference betweenxandy. -
x*y: This provides the product ofxandy. -
x/y: This provides the quotient ofxandy. -
x%y: This provides the remainder ofxandyafter division. -
x++: The value ofxis increased by one. -
x--: The value ofxis decreased by one.
Example
package mainimport ("fmt")func main() {a := 10b := 8var sum = a + bfmt.Println(sum)var diff = a - bfmt.Println(diff)var product = a * bfmt.Println(product)var division = a/bfmt.Println(division)var module = a%bfmt.Println(module)a++fmt.Println(a)b--fmt.Println(b)}
Explanation
- Line 8: We assign the value,
10, to the variablea. - Line 9: We assign the value,
8, to the variableb. - Line 11: This is the syntax to add
aandb. - Line 14: This is the syntax to subtract
aandb. - Line 17: This is the syntax to multiply
aandb. - Line 20: This is the syntax to divide
aandb. - Line 23: This is the syntax to find the remainder of
aandb. - Line 26: This is the value of
aincreased by one. - Line 29: This is the value of
bdecreased by one.
Free Resources