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.
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) }
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)
What is the difference if we define:
type deque struct { indexes *[]int }
For example, the address of the deque instance in memory is 0x1001
.
It initializes:
0x1001 -> [indexes: nil]
If you add a new element (dq.push(12)
):
0x1001 -> [length: 1, capacity: 1, data: 0x3001 (pointer to data)] 0x3001 -> [12]
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]). p>
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]
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) }
Output 1:
Deque elements: [12 34 56]
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]
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]
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) }
Output 2:
Deque elements: [12 34 56]
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!