Go Operators
Estimated reading: 3 minutes 30 views

📝 Go Assignment Operators – Assign and Update Values in Go (2025 Guide)

🧲 Introduction – What Are Assignment Operators in Go?

Assignment operators in Go are used to store values into variables and optionally modify them in the same step. They form the foundation of programming logic — whether you’re counting, calculating, or updating data structures.

🎯 In this section, you’ll learn:

  • The complete list of assignment operators in Go
  • Difference between = and +=, -=, etc.
  • Real examples with numeric and string types
  • Common use cases and best practices

➕ List of Assignment Operators in Go

OperatorDescriptionExampleEquivalent To
=Assigns a valuex = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 2x = x - 2
*=Multiply and assignx *= 4x = x * 4
/=Divide and assignx /= 2x = x / 2
%=Modulus and assignx %= 3x = x % 3

These operators modify the existing variable value and then store the result back in the same variable.


✏️ Syntax – Basic Assignment in Go

var a int
a = 10  // assignment

Or using shorthand declaration:

b := 20  // declare and assign

➕ Compound Assignment Examples

package main

import "fmt"

func main() {
    x := 10
    x += 5     // x = 15
    x -= 3     // x = 12
    x *= 2     // x = 24
    x /= 6     // x = 4
    x %= 3     // x = 1

    fmt.Println("Final value of x:", x)
}

📤 Output:

Final value of x: 1

🔤 Assignment with Strings

You can only use the = operator for string assignments — no += for appending unless you explicitly reassign.

greeting := "Hello"
greeting += ", Go!"  // Appends string
fmt.Println(greeting)

📤 Output:

Hello, Go!

+= works with strings to concatenate in Go.


🧪 Real-World Example – Score Calculator

score := 0
score += 10  // bonus
score += 20  // level up
score -= 5   // penalty
fmt.Println("Final Score:", score)

📤 Output:

Final Score: 25

🚫 Common Mistakes

MistakeFix
Using := multiple times for the same variableUse = after the first declaration
Mixing types in assignmentConvert types explicitly using float64(x)
Using operators without initializationAlways declare a variable first

📌 Summary – Recap & Next Steps

Go’s assignment operators are concise, readable, and work similarly to other C-style languages. Whether you’re dealing with numbers or strings, assignment expressions help streamline code and logic.

🔍 Key Takeaways:

  • Use = for assigning a value to a variable
  • Use +=, -=, *=, /=, %= to update and assign in one step
  • Compound assignment works with numbers and strings
  • Be cautious of type mismatches and uninitialized variables

⚙️ Next: Explore Go Relational Operators like ==, !=, <, > to compare values in conditions and loops.


❓ FAQs – Go Assignment Operators

❓ Can I use += with strings in Go?
✅ Yes. += works for string concatenation in Go:

s := "Hi"
s += " there"  // "Hi there"

❓ Is there an increment (++) assignment in Go?
✅ Yes, but only as a statement, not an expression:

x++  // valid
y := x++  // ❌ invalid in Go

❓ What happens if I assign a float to an int variable?
✅ You’ll get a compile-time error. You must cast explicitly:

var a int = int(3.14)

❓ Can I reassign a constant in Go?
✅ No. Constants are immutable. You can only assign them once.

❓ What’s the difference between = and :=?
= is for assignment to existing variables. := is shorthand for declare and assign (only inside functions).


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go Assignment Operators

Or Copy Link

CONTENTS
Scroll to Top