有人可以告诉我为什么结果是这样的吗?

huangapple go评论81阅读模式
英文:

Can anyone tell me why the result is like this

问题

import "fmt"

type MyStruct struct {
	Queue []string
}

func main() {

	myStruct := &MyStruct{}
	// 预先分配容量为5的切片
	queue := make([]string, 0, 5)

	myStruct.Queue = queue

	// 将元素添加到队列的“末尾”
	myStruct.Queue = append(myStruct.Queue, "1")
	myStruct.Queue = append(myStruct.Queue, "2")
	myStruct.Queue = append(myStruct.Queue, "3")

	// 队列: [1 2 3]
	fmt.Println(myStruct.Queue)
	fmt.Println(queue)

	// 添加另一个元素
	queue = append(queue, "4")
	fmt.Println(queue)

	queue = append(queue, "5")
	fmt.Println(queue)

	// 队列: [1 2 3] ? 或者 [1 2 3 4 5] ?
	fmt.Println(myStruct.Queue)
	// [4 5 3]
}

我预期的结果是 [1,2,3] 或者 [1,2,3,4,5],但实际结果是 [4, 5, 3]。
当我将 []string 的容量设为 0 时,结果是 [1, 2, 3]。这是因为切片的引用指针地址发生了变化([]string 的长度大于容量)。

顺便说一下,我不明白为什么结果是 [4, 5, 3]。

英文:
import "fmt"

type MyStruct struct {
	Queue []string
}

func main() {

	myStruct := &MyStruct{}
	// Pre-allocate a slice with a capacity of 5
	queue := make([]string, 0, 5)

	myStruct.Queue = queue

	// Add elements to the "end" of the queue
	myStruct.Queue = append(myStruct.Queue, "1")
	myStruct.Queue = append(myStruct.Queue, "2")
	myStruct.Queue = append(myStruct.Queue, "3")

	// queue: [1 2 3]
	fmt.Println(myStruct.Queue)
	fmt.Println(queue)

	// Add another element
	queue = append(queue, "4")
	fmt.Println(queue)

	queue = append(queue, "5")
	fmt.Println(queue)

	// queue: [1 2 3] ? or [1 2 3 4 5] ?
	fmt.Println(myStruct.Queue)
    // [ 4 5 3 ]
}

I expected [1,2,3] or [1,2,3,4,5] but, its [4, 5, 3]
when I changed Capacity of []string as 0, it was [1 ,2, 3]. because its reference pointer address change ([]string length > capacity).

by the way, I don't understand what its [4, 5, 3]

答案1

得分: 2

队列 := make([]string, 0, 5)

创建一个大小为5的匿名数组,并创建指向该数组开头的切片队列,容量为5,长度为0。

myStruct.Queue = 队列

将切片头队列复制到myStruct.Queue中。myStruct.Queue现在指向相同数组的开头,容量为5,长度为0。

myStruct.Queue = append(myStruct.Queue, "1")
myStruct.Queue = append(myStruct.Queue, "2")
myStruct.Queue = append(myStruct.Queue, "3")

myStruct.Queue追加三个元素。由于有空间可用,这些元素被插入到现有数组中,现在数组包含["1" "2" "3" "" ""]myStruct.Queue的长度现在为3。队列仍然指向相同的数组,但其长度仍为0。

队列 = append(队列, "4")
队列 = append(队列, "5")

队列追加两个元素。由于备用数组中有空间,并且队列的长度为零,所以这两个元素被插入到数组的前两个索引中,覆盖了"1"和"2"。

现在数组包含["4" "5" "3" "" ""]队列是对该数组的一个视图,长度为2,因此它包含["4" "5"]myStruct.Queue也是对同一数组的一个视图,长度为3,因此它包含["4" "5" "3"]

英文:
queue := make([]string, 0, 5)

Makes an anonymous array with a size of 5, and creates the slice queue that points to the beginning of that array, has a capacity of 5, and a length of 0.

myStruct.Queue = queue

Copies the slice-header queue into myStruct.Queue. myStruct.Queue now points to the beginning of the same array, has a capacity of 5, and a length of 0.

myStruct.Queue = append(myStruct.Queue, "1")
myStruct.Queue = append(myStruct.Queue, "2")
myStruct.Queue = append(myStruct.Queue, "3")

Appends three elements to myStruct.Queue. Since space is available, these elements are inserted into the existing array, which now contains ["1" "2" "3" "" ""]. myStruct.Queue's length is now 3. queue still points to the same array, but its length is still 0.

queue = append(queue, "4")
queue = append(queue, "5")

Appends two elements to queue. Space is available in the backing array, and queue has a length of zero, so these two elements are inserted into the first two indices of the array, overwriting "1" and "2".

Now the array contains ["4" "5" "3" "" ""]. queue is a view into that array with a length of 2, so it contains ["4" "5"]. myStruct.Queue is a view into the same array with a length of 3, so it contains ["4" "5" "3"].

huangapple
  • 本文由 发表于 2023年7月14日 15:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76685600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定