Go-arithmetic-operators

提供:Dev Guides
移動先:案内検索

Go-算術演算子

次の表に、Go言語でサポートされているすべての算術演算子を示します。 変数 A が10を保持し、変数 B が20を保持すると仮定すると、

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

Goプログラミング言語で使用可能なすべての算術演算子を理解するには、次の例を試してください-

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 )
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

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