Start learning free

LESSON 12 OF 338 · VARIABLES

Variable Reassignment in Go: = vs :=

Lesson Read the full explanation

You can change the value stored in a variable with the assignment operator =.

myVar := 5
myVar = 10

fmt.Println(myVar) // Output: 10

The first line creates myVar. The second line reassigns it to a new value.


Assigning the value of one variable to another:

myVar := 5
myNum := 10

myVar = myNum

fmt.Println(myVar) // Output: 10

After the assignment, myVar has the same value as myNum.


Assignment
Follow the Coding Tutorial and reassign the variables in the marked area.


Hint
Use =, not :=, because the variables already exist.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · VARIABLES

    Variable Reassignment in Go, explained

    Changing a stored value

    A variable is a named box, and the value in the box can be replaced. You do that with the assignment operator =. The first line below creates myVar. The second line stores a new value in it:

    myVar := 5
    myVar = 10
    
    fmt.Println(myVar) // Output: 10
    

    Printing it afterwards shows 10, since the program only ever sees the latest value. The old value, 5, is gone. A variable holds one value at a time, and the most recent assignment wins.

    Declaring versus assigning

    Go uses two different operators here, and mixing them up is the most common error in this topic.

    • := declares a variable that doesn't exist yet and gives it a first value.
    • = assigns a new value to a variable that already exists.

    If you use := on a name that is already declared in the same scope, Go rejects it, because nothing new would be created:

    age := 24
    age := 34
    
    no new variables on left side of :=
    

    The fix is a plain age = 34. The opposite mistake also fails: assigning with = to a name that was never declared gives an undefined error.

    Copying one variable into another

    The right-hand side of an assignment can be any expression, including another variable. Go copies the value:

    myVar := 5
    myNum := 10
    
    myVar = myNum
    
    fmt.Println(myVar) // Output: 10
    

    After the assignment, myVar has the same value as myNum. The two are separate copies. If you later set myNum = 99, myVar stays at 10.

    Swapping two values

    Go can assign to several variables in one statement. All the right-hand values are worked out before any assignment happens, so swapping two variables needs no temporary one:

    a, b := 1, 2
    a, b = b, a
    fmt.Println(a, b) // 2 1
    

    The type stays the same

    You can change a variable's value but not its type. An int variable can only receive integers:

    age := 24
    age = "old"
    
    cannot use "old" (untyped string constant) as int value in assignment
    

    Using the old value

    The new value can be computed from the old one. In age = age + 10, Go evaluates the right side first, using the current value, and then stores the result. The shorthand age += 10 means the same thing.

    A quiet trap: := inside a block

    The rule "no new variables" applies to one scope. Inside an inner block, such as the body of an if, the name is free to be declared again, and := there creates a separate variable that hides the outer one:

    age := 24
    if true {
        age := 34
        fmt.Println("inside", age)
    }
    fmt.Println("outside", age)
    
    inside 34
    outside 24
    

    The outer age never changed. If your intention was to update it, write age = 34 inside the block.

    One more detail: := is allowed with several names on the left as long as at least one is new. In a, c := 5, 6, if a already exists, it gets the value 5 and only c is created.

    The exercise, step by step

    The program tells the story of a developer today, then ten years later. The starter code declares role as "Junior Developer", age as 24 and language as "Java", then prints them. After a marked area, it prints them again. Your code goes in the marked area, between the two sets of prints.

    1. Change the role. He is now a senior developer, so write role = "Senior Developer". Use =, because role exists.
    2. Change the age. Ten years have passed, so 24 becomes 34: age = 34.
    3. Leave language alone. He still codes in Java, and the second set of prints already says "still".
    role := "Junior Developer"
    age := 24
    language := "Java"
    
    // Today
    fmt.Println("His age is " + strconv.Itoa(age))
    fmt.Println("He is a " + role)
    fmt.Println("He codes in " + language)
    
    // ---------------------------------------
    // Write your code here:
    
    role = "Senior Developer"
    age = 34
    
    // ---------------------------------------
    
    // Ten years later:
    fmt.Println("Ten years later, his age is " + strconv.Itoa(age))
    fmt.Println("He is a " + role)
    fmt.Println("He still codes in " + language)
    

    The first group of prints shows the old values and the last group the new ones: 34 and Senior Developer, with Java unchanged.

    Reading the marked area as a story helps: the code above it describes the past, and the assignments update the facts before the final prints describe the present.

    Key takeaways

    • := creates a variable; = changes one that exists.
    • Using := with no new name on the left is a compile error.
    • Assignment replaces the old value, and the variable keeps its type.
    • Assigning one variable to another copies the value.
    • x = x + 10 and x += 10 update a variable from its own value.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free