Home > Backend Development > Golang > How to understand slices in Golang structures

How to understand slices in Golang structures

WBOY
Release: 2024-02-08 21:45:12
forward
950 people have browsed it

如何理解 Golang 结构体中的切片

In Golang, a structure is a custom data type that can contain multiple fields. In a structure, a slice is a dynamic array that can be expanded or reduced as needed. However, understanding slices in Golang structs can cause some confusion. In this article, PHP editor Youzi will explain to you in detail the working principle and usage of slicing in the Golang structure, helping you better understand and apply this feature. Whether you are a beginner or an experienced developer, this article will provide you with valuable knowledge and practical tips to make you more comfortable in Golang development.

Question content

I am new to Golang and I am trying to understand pointers

type deque struct {
    indexes []int
}

func (d *deque) push(i int) {
    d.indexes = append(d.indexes, i)
}
Copy after login

The index here is a slice rather than a pointer to the slice.

How are indexes actually stored in memory?

For example: When we start a deque instance, we call it dq. In memory, the address of dq is 0x1001 (we call it adr(dq)).

What are the variables stored in adr(dq)? Is it a pointer to an array?

0x1001 -> Ox8009 (the address of the first element of the array)

Or the array itself?

0x1001 -> The first element of the slice

0x1002 -> The second element of the slice

What happens when we:

d.indexes = append(d.indexes, i)
Copy after login

What is the difference if we define:

type deque struct {
    indexes *[]int
}
Copy after login

Solution

For example, the address of the deque instance in memory is 0x1001. It initializes:

0x1001 -> [indexes: nil]
Copy after login

If you add a new element (dq.push(12)):

0x1001 -> [length: 1, capacity: 1, data: 0x3001 (pointer to data)]
0x3001 -> [12]
Copy after login
The slice structure at

0x1001 contains information about the slice length and capacity (length: 1, capacity: 1), the actual data is stored at another address (assuming 0x3001, array[12]).

If you push other elements (dq.push(34), dq.push(56)).

0x1001 -> [length: 3, capacity: 4, data: 0x5001 (new pointer to data due to capacity, capacity doubles)]
0x5001 -> [12, 34, 56]
Copy after login

Code 1:

type deque struct {
    indexes []int
}
func (d *deque) push(i int) {
    d.indexes = append(d.indexes, i)
}
func main() {
    dq := deque{}
    dq.push(12)
    dq.push(34)
    dq.push(56)
    fmt.Println("Deque elements:", dq.indexes)
}
Copy after login

Output 1:

Deque elements: [12 34 56]
Copy after login
Copy after login

If using a pointer to a slice (*[]int) instead of the slice itself ([]int). This means that the indexes field will hold a pointer to the slice, and that pointer needs to be initialized before using it.

If you add a new element (dq.push(12)), then 0x2001 is the address of the underlying slice structure pointed to by indexes

0x1001 -> [indexes: 0x2001 (pointer to slice)]
0x2001 -> [length: 1, capacity: 1, data: 0x3001 (pointer to data)]
0x3001 -> [12]
Copy after login
The slice structure at

0x2001 contains the length and capacity information of the slice (length: 1, capacity: 1), and the actual data is stored at another address (0x3001, array [12]).

If you push other elements (dq.push(34), dq.push(56)).

0x1001 -> [indexes: 0x2001 (pointer to slice)]
0x2001 -> [length: 3, capacity: 4, data: 0x5001 (new data pointer, due to capacity, capacity doubles)]
0x5001 -> [12, 34, 56]
Copy after login

Code 2:

type deque struct {
    indexes *[]int
}

func (d *deque) push(i int) {
    if d.indexes == nil {    // initialize the slice if it's nil
        d.indexes = &[]int{}
    }
    *d.indexes = append(*d.indexes, i)
}

func main() {
    dq := deque{}
    dq.push(12)
    dq.push(34)
    dq.push(56)
    fmt.Println("Deque elements:", *dq.indexes)
}
Copy after login

Output 2:

Deque elements: [12 34 56]
Copy after login
Copy after login

The above is the detailed content of How to understand slices in Golang structures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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