1. package main
    2. import (
    3. "fmt"
    4. )
    5. func main() {
    6. filtered := make([]*student, 10)
    7. fmt.Printf("filtered: len=%v cap=%v \n", len(filtered), cap(filtered))
    8. for index, val := range filtered {
    9. fmt.Println("index: ", index, "val: ", val)
    10. }
    11. }

    输出:

    1. filtered: len=10 cap=10
    2. index: 0 val: <nil>
    3. index: 1 val: <nil>
    4. index: 2 val: <nil>
    5. index: 3 val: <nil>
    6. index: 4 val: <nil>
    7. index: 5 val: <nil>
    8. index: 6 val: <nil>
    9. index: 7 val: <nil>
    10. index: 8 val: <nil>
    11. index: 9 val: <nil>

    可以看到filtered长度不为0,for循环就会遍历。不指定cap时,默认len和cap相等。

    1. package main
    2. import (
    3. "fmt"
    4. )
    5. func main() {
    6. filtered := make([]*student, 10)
    7. fmt.Printf("filtered: len=%v cap=%v \n", len(filtered), cap(filtered))
    8. for index, val := range filtered {
    9. fmt.Println("index: ", index, "val: ", val)
    10. filtered[index] = &student{
    11. ID: index,
    12. Name: fmt.Sprintf("%v", index),
    13. }
    14. }
    15. fmt.Printf("filter[8]=%v, addr=%p\n", filtered[8], &filtered[8])
    16. aa := filtered[8]
    17. fmt.Printf("aa=%v, addr=%p\n", aa, &aa)
    18. aa.Name = "test coderaction"
    19. fmt.Printf("filter[8]=%v, addr=%p\n", filtered[8], &filtered[8])
    20. fmt.Printf("aa=%v, addr=%p\n", aa, &aa)
    21. }
    22. type student struct {
    23. ID int
    24. Name string
    25. }
    26. func getPods() []*student {
    27. stu1 := &student{
    28. ID: 1,
    29. Name: "stu1",
    30. }
    31. stu2 := &student{
    32. ID: 2,
    33. Name: "stu2",
    34. }
    35. return []*student{stu1, stu2}
    36. }

    输出:

    1. filtered: len=10 cap=10
    2. index: 0 val: <nil>
    3. index: 1 val: <nil>
    4. index: 2 val: <nil>
    5. index: 3 val: <nil>
    6. index: 4 val: <nil>
    7. index: 5 val: <nil>
    8. index: 6 val: <nil>
    9. index: 7 val: <nil>
    10. index: 8 val: <nil>
    11. index: 9 val: <nil>
    12. filter[8]=&{8 8}, addr=0xc0000a4040
    13. aa=&{8 8}, addr=0xc00009c020
    14. filter[8]=&{8 test coderaction}, addr=0xc0000a4040
    15. aa=&{8 test coderaction}, addr=0xc00009c020

    可以看到修改了aa之后,filtered[8]的内容也变掉了。

    给aa新赋值&student{},发现filtered[8]是没有变化的:

    1. package main
    2. import (
    3. "fmt"
    4. )
    5. func main() {
    6. filtered := make([]*student, 10)
    7. fmt.Printf("filtered: len=%v cap=%v \n", len(filtered), cap(filtered))
    8. for index, val := range filtered {
    9. fmt.Println("index: ", index, "val: ", val)
    10. filtered[index] = &student{
    11. ID: index,
    12. Name: fmt.Sprintf("%v", index),
    13. }
    14. }
    15. fmt.Printf("filter[8]=%v, addr=%p\n", filtered[8], &filtered[8])
    16. aa := filtered[8]
    17. fmt.Printf("aa=%v, addr=%p\n", aa, &aa)
    18. aa = &student{
    19. ID: 1001,
    20. Name: "aaaa",
    21. }
    22. fmt.Printf("filter[8]=%v, addr=%p\n", filtered[8], &filtered[8])
    23. fmt.Printf("aa=%v, addr=%p\n", aa, &aa)
    24. }
    25. type student struct {
    26. ID int
    27. Name string
    28. }

    可以看到aa的地址和filtered[8]不一样:

    1. filtered: len=10 cap=10
    2. index: 0 val: <nil>
    3. index: 1 val: <nil>
    4. index: 2 val: <nil>
    5. index: 3 val: <nil>
    6. index: 4 val: <nil>
    7. index: 5 val: <nil>
    8. index: 6 val: <nil>
    9. index: 7 val: <nil>
    10. index: 8 val: <nil>
    11. index: 9 val: <nil>
    12. filter[8]=&{8 8}, addr=0xc000090040
    13. aa=&{8 8}, addr=0xc000006030
    14. filter[8]=&{8 8}, addr=0xc000090040
    15. aa=&{1001 aaaa}, addr=0xc000006030