Go 切片绕坑指南

KevinYan · · 224 次点击 · · 开始浏览

测验一

``````func main() {
var s []int
for i := 1; i <= 3; i++ {
s = append(s, i)
}
reverse(s)
fmt.Println(s)
}

func reverse(s []int) {
for i, j := 0, len(s) - 1; i < j; i++ {
j = len(s) - (i + 1)
s[i], s[j] = s[j], s[i]
}
}``````

Run it on the Go Playground → https://play.golang.org/p/faJ...

测验二

``````func main() {
var s []int
for i := 1; i <= 3; i++ {
s = append(s, i)
}
reverse(s)
fmt.Println(s)
}

func reverse(s []int) {
s = append(s, 999)
for i, j := 0, len(s) - 1; i < j; i++ {
j = len(s) - (i + 1)
s[i], s[j] = s[j], s[i]
}
}``````

Run it on the Go Playground → https://play.golang.org/p/tZp...

``````type slice struct {
array unsafe.Pointer
len   int
cap   int
}``````

最终测验

``````func main() {
var s []int
for i := 1; i <= 3; i++ {
s = append(s, i)
}
reverse(s)
fmt.Println(s)
}

func reverse(s []int) {
s = append(s, 999, 1000, 1001)
for i, j := 0, len(s)-1; i < j; i++ {
j = len(s) - (i + 1)
s[i], s[j] = s[j], s[i]
}
}``````

Run it on the Go Playground → https://play.golang.org/p/dnb...

通过 cap 函数验证我们的结论

``````func reverse(s []int) {
newElem := 999
for len(s) < cap(s) {
fmt.Println("Adding an element:", newElem, "cap:", cap(s), "len:", len(s))
s = append(s, newElem)
newElem++
}
for i, j := 0, len(s)-1; i < j; i++ {
j = len(s) - (i + 1)
s[i], s[j] = s[j], s[i]
}
}``````

Run it on the Go Playground → https://play.golang.org/p/SBH...

这个问题不仅限于切片类型

``````type A struct {
Ptr1 *B
Ptr2 *B
Val B
}

type B struct {
Str string
}

func main() {
a := A{
Ptr1: &B{"ptr-str-1"},
Ptr2: &B{"ptr-str-2"},
Val: B{"val-str"},
}
fmt.Println(a.Ptr1)
fmt.Println(a.Ptr2)
fmt.Println(a.Val)
demo(a)
fmt.Println(a.Ptr1)
fmt.Println(a.Ptr2)
fmt.Println(a.Val)
}

func demo(a A) {
// Update a value of a pointer and changes will persist
a.Ptr1.Str = "new-ptr-str1"
// Use an entirely new B object and changes won't persist
a.Ptr2 = &B{"new-ptr-str-2"}
a.Val.Str = "new-val-str"
}``````

Run it on the Go Playground → https://play.golang.org/p/8X-...

``````type slice struct {
array unsafe.Pointer
len   int
cap   int
}``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

测验一

``````func main() {
var s []int
for i := 1; i <= 3; i++ {
s = append(s, i)
}
reverse(s)
fmt.Println(s)
}

func reverse(s []int) {
for i, j := 0, len(s) - 1; i < j; i++ {
j = len(s) - (i + 1)
s[i], s[j] = s[j], s[i]
}
}``````

Run it on the Go Playground → https://play.golang.org/p/faJ...

测验二

``````func main() {
var s []int
for i := 1; i <= 3; i++ {
s = append(s, i)
}
reverse(s)
fmt.Println(s)
}

func reverse(s []int) {
s = append(s, 999)
for i, j := 0, len(s) - 1; i < j; i++ {
j = len(s) - (i + 1)
s[i], s[j] = s[j], s[i]
}
}``````

Run it on the Go Playground → https://play.golang.org/p/tZp...

``````type slice struct {
array unsafe.Pointer
len   int
cap   int
}``````

最终测验

``````func main() {
var s []int
for i := 1; i <= 3; i++ {
s = append(s, i)
}
reverse(s)
fmt.Println(s)
}

func reverse(s []int) {
s = append(s, 999, 1000, 1001)
for i, j := 0, len(s)-1; i < j; i++ {
j = len(s) - (i + 1)
s[i], s[j] = s[j], s[i]
}
}``````

Run it on the Go Playground → https://play.golang.org/p/dnb...

通过 cap 函数验证我们的结论

``````func reverse(s []int) {
newElem := 999
for len(s) < cap(s) {
fmt.Println("Adding an element:", newElem, "cap:", cap(s), "len:", len(s))
s = append(s, newElem)
newElem++
}
for i, j := 0, len(s)-1; i < j; i++ {
j = len(s) - (i + 1)
s[i], s[j] = s[j], s[i]
}
}``````

Run it on the Go Playground → https://play.golang.org/p/SBH...

这个问题不仅限于切片类型

``````type A struct {
Ptr1 *B
Ptr2 *B
Val B
}

type B struct {
Str string
}

func main() {
a := A{
Ptr1: &B{"ptr-str-1"},
Ptr2: &B{"ptr-str-2"},
Val: B{"val-str"},
}
fmt.Println(a.Ptr1)
fmt.Println(a.Ptr2)
fmt.Println(a.Val)
demo(a)
fmt.Println(a.Ptr1)
fmt.Println(a.Ptr2)
fmt.Println(a.Val)
}

func demo(a A) {
// Update a value of a pointer and changes will persist
a.Ptr1.Str = "new-ptr-str1"
// Use an entirely new B object and changes won't persist
a.Ptr2 = &B{"new-ptr-str-2"}
a.Val.Str = "new-val-str"
}``````

Run it on the Go Playground → https://play.golang.org/p/8X-...

``````type slice struct {
array unsafe.Pointer
len   int
cap   int
}``````