Slice使用的一个问题

buaatianwanli · · 1778 次点击
package main import ( "fmt" "sync" ) type Data struct { A []int32 B []string } func main() { channel := make(chan Data) data := Data{A: make([]int32, 1, 1), B: make([]string, 1, 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) }
#11
更多评论
各位大拿给出宝贵意见
#1
hua666777
程序猿钱多活少。
看起来好难1
#2