package main
import (
"fmt"
"sync"
)
type Data struct {
A []int32
B []string
}
func main() {
channel := make(chan Data)
data := Data{A: make([]int32, 0, 1), B: make([]string, 0, 1)}
fmt.Printf("Before gorountine addr, A: %p\n", data.A)
fmt.Printf("Before gorountine addr, B: %p\n", data.B)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
for d := range channel {
fmt.Printf("goroutine addr before append, A: %p\n", d.A)
fmt.Printf("goroutine addr before append, B: %p\n", d.B)
d.A = append(d.A, 23)
d.B = append(d.B, "23")
//d.A[0] = 23
//d.B[0] = "23"
fmt.Printf("goroutine addr after append, A: %p\n", d.A)
fmt.Printf("goroutine addr after append, B: %p\n", d.B)
fmt.Println(d)
}
wg.Done()
}()
fmt.Printf("Before: %v\n", data)
channel <- data
close(channel)
wg.Wait()
fmt.Printf("After gorountine addr, A: %p\n", data.A)
fmt.Printf("After gorountine addr, B: %p\n", data.B)
fmt.Printf("After: %v\n", data)
}
代码见上
输出见下:
Before gorountine addr, A: 0xc20800a1dc
Before gorountine addr, B: 0xc20800a200
Before: {[] []}
goroutine addr before append, A: 0xc20800a1dc
goroutine addr before append, B: 0xc20800a200
goroutine addr after append, A: 0xc20800a1dc
goroutine addr after append, B: 0xc20800a200
{[23] [23]}
After gorountine addr, A: 0xc20800a1dc
After gorountine addr, B: 0xc20800a200
After: {[] []}
我的问题是:为什么slice的地址没有变,但是master goroutine里的data数据为空?
如果我去掉append,改为直接赋值,最后data则不为空
需要注意的是,我在make slice的时候特地设置cap为1
我明白了,这个问题不是struct的值对应的slice是否变化的问题,本质是slice的底层数组表示。
你可能也注意到了,如果使用s = append(s, 1)和s[0]=1结果不太一样,对吧?
请看这段代码:
```
package main
import (
"fmt"
)
func main() {
var s []int
fmt.Printf("\t %v - %p - %p\n", s, s, &s)
s = make([]int, 0, 1)
fmt.Printf("\t %v - %p - %p\n", s, s, &s)
s = append(s, 1)
fmt.Printf("\t %v - %p - %p\n", s, s, &s)
copy := s
fmt.Printf("\t %v - %p - %p\n", copy, copy, &copy)
copy[0] = 2
fmt.Printf("\t %v - %p - %p\n", copy, copy, &copy)
fmt.Printf("\t %v - %p - %p\n", s, s, &s)
}
```
运行结果是:
```
[] - 0x0 - 0xc820056080
[] - 0xc82005c1c0 - 0xc820056080
[1] - 0xc82005c1c0 - 0xc820056080
[1] - 0xc82005c1c0 - 0xc820056180
[2] - 0xc82005c1c0 - 0xc820056180
[2] - 0xc82005c1c0 - 0xc820056080
```
也就是被copy变量和原变量是共用了底层数组,所以修改copy之后,原来的slice也会被修改(直到超出容量之后重新分配底层数组)。
#13