Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[勘误]反馈一个第2章第2.2节的示例运行结果错误 #101

Open
ocaraworks opened this issue Dec 21, 2021 · 3 comments
Open

[勘误]反馈一个第2章第2.2节的示例运行结果错误 #101

ocaraworks opened this issue Dec 21, 2021 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@ocaraworks
Copy link

ocaraworks commented Dec 21, 2021

问题描述
slice原理一节的2.2示例的运行结果和解释错误。

如何找到这个错误

  • 章节:2.2 题目二
  • 页码:

您认为应该如何?
false

图片
如果有可能,尽量提供图片。

其他补充信息

示例的源代码:

package main

import (
"fmt"
)

func AddElement(slice []int, e int) []int {
return append(slice, e)
}

func main() {
var slice []int
slice = append(slice, 1, 2, 3)

newSlice := AddElement(slice, 4)
fmt.Println(&slice[0] == &newSlice[0])

}

错误原因:
因为AddElement函数内部append重新扩容了,但是它和原来的slice是两个内存地址值。

注:我的golang 1.16.7版本

@ocaraworks ocaraworks added the bug Something isn't working label Dec 21, 2021
@ocaraworks
Copy link
Author

这个错误。请运行核查。

@RainbowMango
Copy link
Owner

题目二?
是这个吗?

下面函数输出什么?
```go
func SliceRise(s []int) {
	s = append(s, 0)
	for i := range s {
		s[i]++
	}
}

func SlicePrint() {
	s1 := []int{1, 2}
	s2 := s1
	s2 = append(s2, 3)
	SliceRise(s1)
	SliceRise(s2)
	fmt.Println(s1, s2)
}

单选:

  • A: [2, 3] [2, 3, 4]
  • B: [1, 2] [1, 2, 3]
  • C: [1, 2] [2, 3, 4]
  • D: [2, 3, 1] [2, 3, 4, 1]

PS: 不知道你看的是第几次印刷的版本,有点对照不上。

@RainbowMango
Copy link
Owner

感谢描述的是这个问题 #88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants