What is the difference between `array` and `slice{array,array,...}` in Golang?

WBOY
Release: 2024-02-08 23:10:10
forward
1142 people have browsed it

Golang 中 `array` 和 `slice{array,array,...}` 有什么区别?

What is the difference between `array` and `slice{array,array,...}` in Golang? This is a question often asked by many beginners. PHP editor Zimo will answer for you: In Golang, `array` is a fixed-length sequence. Once defined, the length cannot be changed. `slice` is a sequence of dynamic length that can be expanded or reduced as needed. In addition, the length of `array` is determined at definition time, while the length of `slice` can be changed dynamically at runtime. Therefore, when using it, you need to choose an appropriate data structure based on actual needs.

Question content

I would like to know why, please give me a hint.

I want to append an array to res, res is a 2D slice. So I need to convert first. When I convert the array to slice, I get an error.

// i need a map to remove duplicates
mm := map[[3]int]bool{}
mm[[3]int{-1, -1, 2}] = true
mm[[3]int{-1, -1, 2}] = true
mm[[3]int{-1, 0, 1}] = true

var res [][]int
for k, _ := range mm {
    res = append(res, k[:])
}
fmt.printf("the res is %v\n", res)
Copy after login
the res is [[-1 0 1] [-1 0 1]]
Copy after login

But the result is not what I want.

Then I tentatively modified the for loop

for k, _ := range mm {
    //res = append(res, k[:])
    res = append(res, []int{k[0], k[1], k[2]})
}
Copy after login
the res is [[-1 0 1] [-1 -1 2]]
Copy after login

Now the result is correct, but why? What is the difference between k[:] and []int{k[0],k[1],k[2]}?

Solution

Change the loop to

for k, _ := range mm {
    j := k
    res = append(res, j[:])
}
Copy after login

Your original loop declares a variable k of type [3]int that has a specific location in memory. Each iteration of the loop, a different key from the map mm is copied to this variable. So far, so good.

When you convert it to a slice using k[:], it creates a slice header that points to the array k. Something went wrong here - the next iteration of the loop, the value of k was overwritten. All slices created in the loop point to the same backing array k at the same location in memory.

You avoid the problem by giving each slice its own backing array by first copying the value of k to a variable declared inside the loop.

The above is the detailed content of What is the difference between `array` and `slice{array,array,...}` in Golang?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template