ProgramGuru

Updating Array Elements

Start with Updating Array Elements

An array element can be replaced using an indexed assignment. The array length stays fixed while its values change.

What you’ll learn

  • Explain the main purpose of updating array elements.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Write points[1] = 42 to replace the second element. Indexing can also appear on the left of compound assignments such as points[0] += 5. Each assignment requires the new value to be compatible with the array element type.

When an array is passed by value to a function, the function gets a copy. Updating its parameter does not change the caller’s original array. You can return the changed copy or use a pointer when you intentionally need shared mutation.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for update arrayIndexed writes affect the particular array value being changed.main owns arrayStep 1Call with copyStep 2Modify local copyStep 3main unchangedStep 4Indexed writes affect the particular array value being changed.
Indexed writes affect the particular array value being changed.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func changeCopy(values [3]int) {
	values[0] = 100
	fmt.Println("Inside:", values)
}
func main() {
	values := [3]int{5, 10, 15}
	values[1] = 20
	changeCopy(values)
	fmt.Println("Outside:", values)
}

Output

Inside: [100 20 15]
Outside: [5 20 15]

Trace what the program does

The assignment changes the second element to 20 in main. Calling changeCopy copies the array. That function sets its local first element to 100 and prints the copy. When it returns, main still owns [5 20 15].

Avoid common mistakes

  • Replacing an element does not resize an array.
  • Passing a regular array argument copies its elements; mutations to the parameter do not update the caller.

Try it yourself

Create a function that returns an updated [3]int array. Assign its result back to the original variable.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Why is Outside unaffected by values[0] = 100 inside changeCopy?
Choose one answer. You can try again.

Answer and explanation

The function parameter is a separate array copy

Correct. The assignment changes the second element to 20 in main.

What you should remember

You have practiced Updating Array Elements in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Multidimensional Arrays →