Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions concepts/arithmetic-operators/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"blurb": "Go has standard arithmetic operators for numeric types.",
"authors": ["jmrunkle"],
"contributors": []
"authors": [
"jmrunkle"
],
"contributors": [
"BNAndras"
]
}
33 changes: 18 additions & 15 deletions concepts/arithmetic-operators/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Operators

Go supports many standard arithmetic operators:
Go supports the following arithmetic operators:

| Operator | Example |
| -------- | -------------- |
Expand All @@ -14,24 +14,29 @@ Go supports many standard arithmetic operators:

For integer division, the remainder is dropped (e.g. `5 / 2 == 2`).

## Arithmetic operations on different types
The `+` operator also concatenates strings: `"foo" + "bar" == "foobar"`.

In many languages you can perform arithmetic operations on different types of variables, but in Go this gives an error.
For example:
## Mixed-Type Arithmetic

Go does not allow arithmetic operations between different numeric types:

```go
var x int = 42
x := 42
n := 2.0
value := n * x // invalid operation: n * x (mismatched types float64 and int)
```

// this line produces an error
value := float32(2.0) * x // invalid operation: mismatched types float32 and int
The operands must be the same type, sometimes requiring explicit conversion:

// you must convert int type to float32 before performing arithmetic operation
value := float32(2.0) * float32(x)
```go
x := float64(42)
n := 2.0
value := n * x
```

## Shorthand Assignments

These can be used in shorthand assignments to update and assign a variable using the operator:
Each operator has a shorthand assignment form that updates the variable in place:

```go
a := 1
Expand All @@ -52,9 +57,8 @@ e %= 2 // same as e = e % 2 == 0

## Increment and Decrement

There are also two special statements: increment (`++`) and decrement (`--`).
They modify the value of a variable by increasing (or decreasing) the value by 1.
For example:
When placed after a variable, the increment (`++`) and decrement (`--`) statements either increase or decrease its stored numeric value by 1.
Placing them before a variable is a syntax error.

```go
a := 10
Expand All @@ -64,5 +68,4 @@ b := 10
b-- // same as b -= 1, b == 9
```

NOTE: these are statements and cannot be used as expressions (ie. they do not return a value).
Also, only the postfix notation is allowed (ie. no `++a` or `--a`).
Because they are statements, they do not return a value.
49 changes: 35 additions & 14 deletions concepts/arithmetic-operators/introduction.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Introduction to Arithmetic Operators
# About Arithmetic Operators

## Operators

Go supports many standard arithmetic operators:
Go supports the following arithmetic operators:

| Operator | Example |
| -------- | -------------- |
Expand All @@ -14,37 +14,58 @@ Go supports many standard arithmetic operators:

For integer division, the remainder is dropped (e.g. `5 / 2 == 2`).

## Arithmetic operations on different types
The `+` operator also concatenates strings: `"foo" + "bar" == "foobar"`.

In many languages you can perform arithmetic operations on different types of variables, but in Go this gives an error.
For example:
## Mixed-Type Arithmetic

Go does not allow arithmetic operations between different numeric types:

```go
var x int = 42
x := 42
n := 2.0
value := n * x // invalid operation: n * x (mismatched types float64 and int)
```

// this line produces an error
value := float32(2.0) * x // invalid operation: mismatched types float32 and int
The operands must be the same type, sometimes requiring explicit conversion:

// you must convert int type to float32 before performing arithmetic operation
value := float32(2.0) * float32(x)
```go
x := float64(42)
n := 2.0
value := n * x
```

## Shorthand Assignments

These can be used in shorthand assignments to update and assign a variable using the operator:
Each operator has a shorthand assignment form that updates the variable in place:

```go
a := 1
a += 2 // same as a = a + 2 == 3

b := 2
b -= 2 // same as b = b - 2 == 0

c := 4
c *= 2 // same as c = c * 2 == 8

d := 8
d /= 2 // same as d = d / 2 == 4

e := 16
e %= 2 // same as e = e % 2 == 0
```

## Increment and Decrement

There are also two special statements: increment (`++`) and decrement (`--`).
They modify the value of a variable by increasing (or decreasing) the value by 1.
For example:
When placed after a variable, the increment (`++`) and decrement (`--`) statements either increase or decrease its stored numeric value by 1.
Placing them before a variable is a syntax error.

```go
a := 10
a++ // same as a += 1, a == 11

b := 10
b-- // same as b -= 1, b == 9
```

Because they are statements, they do not return a value.
Loading