Chapter 27: Assignment
1. What Are Assignment Operators?
Assignment operators are used to assign a value to a variable (or modify an existing value).
In Go there are two kinds:
- The normal assignment=
- The compound assignment operators (+=, -=, *=, etc.)
Go has no:
- ++x / –x as expressions (only as statements)
- &&= / ||= logical compound assignments
- **= power assignment
- multiple assignment in one expression like Python a = b = 5
2. The Basic Assignment Operator: =
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
var count int count = 42 // simple assignment name := "Webliance" // short variable declaration (preferred style inside functions) age, city := 25, "Hyderabad" // multiple assignment (very common) fmt.Println(count, name, age, city) |
Key rules:
- You must use = when declaring with var or when re-assigning an already declared variable
- Short declaration := can only be used inside functions and must introduce at least one new variable
|
0 1 2 3 4 5 6 7 8 9 10 11 |
// This is INVALID outside function global = 100 // compile error // This is valid inside function x := 10 x = 20 // re-assignment with = |
3. All Compound Assignment Operators in Go
These are shorthand for operation + assignment.
| Operator | Equivalent to | Works on | Example | Result after |
|---|---|---|---|---|
| += | a = a + b | numbers, strings | s += “!” | string append |
| -= | a = a – b | numbers | score -= penalty | — |
| *= | a = a * b | numbers | total *= quantity | — |
| /= | a = a / b | numbers | points /= 2 | integer or float division |
| %= | a = a % b | integers only | remainder %= divisor | — |
| &= | a = a & b | integers | flags &= mask | bitwise AND |
|
= | a = a |
b | integers |
| ^= | a = a ^ b | integers | bits ^= toggle | bitwise XOR |
| <<= | a = a << b | integers | value <<= 3 | left shift |
| >>= | a = a >> b | integers | large >>= 1 | right shift |
| &^= | a = a &^ b | integers | flags &^= ForbiddenBit | bit clear (Go-specific) |
No compound assignment for logical operators (&&=, ||=) — intentional design choice.
4. Real Runnable Examples
Create assignment_operators.go and try these:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package main import "fmt" func main() { // Basic assignment score := 100 score = 85 // re-assignment fmt.Println("Score:", score) // Compound arithmetic temperature := 32.5 temperature += 5.2 // 37.7 temperature -= 2.0 // 35.7 temperature *= 1.1 // 39.27 fmt.Printf("Temperature: %.2f °C\n", temperature) // String += message := "Hello" message += " from" message += " Hyderabad!" fmt.Println(message) // Bitwise compound flags := 0b00001111 // 15 flags |= 0b00100000 // set bit 5 → 0b00101111 = 47 flags &^= 0b00000100 // clear bit 2 → 0b00101011 = 43 flags ^= 0b00010000 // toggle bit 4 → 0b00111011 = 59 fmt.Printf("Flags binary: %08b (decimal %d)\n", flags, flags) // Multiple assignment (very common pattern) x, y, z := 10, 20, 30 x, y = y, x // swap without temp variable fmt.Println("After swap:", x, y, z) // Increment / decrement (statements only) counter := 0 counter++ counter++ counter-- fmt.Println("Counter:", counter) // 1 } |
5. Common Beginner Mistakes & How to Avoid Them
- Forgetting to assign back when using compound operators
|
0 1 2 3 4 5 6 7 8 9 10 11 |
// WRONG total += 10 // compile error if total not declared before // Correct var total int total += 10 |
- Trying to use += on untyped constants or wrong types
|
0 1 2 3 4 5 6 7 |
const MAX = 100 // MAX += 10 // error – cannot assign to constant |
- Expecting ++ / — to be usable in expressions
|
0 1 2 3 4 5 6 7 8 9 10 11 |
// WRONG fmt.Println(i++) // error // Correct i++ fmt.Println(i) |
- Using = instead of := when you want short declaration
|
0 1 2 3 4 5 6 7 |
name = "Webliance" // error if name not declared before name := "Webliance" // correct |
6. Quick Practice Questions for You
Try to write one-liners that do the following (predict result first):
- Start with points := 150, subtract 25, then multiply by 2
- Start with status := 0b1010, set the 3rd bit (0-based) using |=
- Start with text := “Go is”, append ” awesome!” using +=
- Swap two variables a=5, b=10 using multiple assignment
Which operator did you use most?
Any part still confusing?
- Why no **= power assignment?
- Why no logical compound assignment?
- Difference between a += b and a = a + b in performance?
- Or ready for comparison operators next?
Keep typing small pieces and printing results — assignment operators are used in literally every non-trivial function you’ll write.
You’re progressing beautifully — keep asking! 💪🇮🇳🚀
