Home > Backend Development > Golang > Why Doesn't Modifying Struct Values in a Go Range Loop Change the Original Slice?

Why Doesn't Modifying Struct Values in a Go Range Loop Change the Original Slice?

Barbara Streisand
Release: 2024-12-20 19:05:11
Original
314 people have browsed it

Why Doesn't Modifying Struct Values in a Go Range Loop Change the Original Slice?

Why Values in a Range of Type Structure Cannot Be Modified

In Go, a range of type structure, like a slice, creates a view of the underlying data rather than a copy. When iterating over a range, any modifications made to the iteration variable affect only that particular view, not the original data.

To understand this, consider the following code snippet:

type myStruct struct {
    Name  string
    Count int
}

func main() {
    chartRecords := []myStruct{}
    for i := 0; i < 4; i++ {
        n := myStruct{Count: i, Name: fmt.Sprintf("Joe%2d", i)}
        chartRecords = append(chartRecords, n)
    }

    for _, elem := range chartRecords {
        elem.Count = 0
        fmt.Println(elem)
    }

    fmt.Println(chartRecords)
}
Copy after login

This code snippet iterates over the range of chartRecords, modifies the Count field of each iteration variable to 0, and prints the modified variable. However, when the original chartRecords slice is printed, it shows that the Count field values have not changed.

This is because the for loop with a range clause creates a copy of each element in the range. Modifications made to the copy do not affect the original element. To modify the original elements, you need to explicitly update them by accessing their index in the slice. For example:

func main() {
    chartRecords := []myStruct{}
    for i := 0; i < 4; i++ {
        n := myStruct{Count: i, Name: fmt.Sprintf("Joe%2d", i)}
        chartRecords = append(chartRecords, n)
    }

    for i, elem := range chartRecords {
        chartRecords[i] = myStruct{Count: 0, Name: elem.Name}
    }

    fmt.Println(chartRecords)
}
Copy after login

In this example, the for loop with a range clause is used to iterate over the range of chartRecords. For each iteration, the index is used to access the original element in the slice, and the element's value is updated. This approach ensures that the modifications are reflected in the original chartRecords slice.

The above is the detailed content of Why Doesn't Modifying Struct Values in a Go Range Loop Change the Original Slice?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template