# One Question When Using Slice

**URL:** <https://forum.golangbridge.org/t/one-question-when-using-slice/2088>\
**Category:** Getting Help\
**Created:** [March 4, 2016, 5:25am UTC](https://forum.golangbridge.org/t/one-question-when-using-slice/2088 "2016-03-04T05:25:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredo\_Lee](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/f/a88e4f/32.png) [@Fredo\_Lee](https://forum.golangbridge.org/u/Fredo_Lee)\
**Post date:** [March 4, 2016, 5:25am UTC](https://forum.golangbridge.org/t/one-question-when-using-slice/2088/1 "2016-03-04T05:25:50Z")

</div>

Code:

```
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)
}

```

We can see that the slice within the struct in master && goroutine process has the same addr.  
but the result is

```
Before gorountine addr, A: 0xc82000a2bc
Before gorountine addr, B: 0xc82000a2e0
Before: {[] []}
goroutine addr before append, A: 0xc82000a2bc
goroutine addr before append, B: 0xc82000a2e0
goroutine addr after append, A: 0xc82000a2bc
goroutine addr after append, B: 0xc82000a2e0
{[23] [23]}
After gorountine addr, A: 0xc82000a2bc
After gorountine addr, B: 0xc82000a2e0
After: {[] []}

```

why the data is empty?

if i remove `append` method, like this:

```
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)
}

```

i can get this:

```
Before gorountine addr, A: 0xc82000a2bc
Before gorountine addr, B: 0xc82000a2e0
Before: {[0] []}
goroutine addr before append, A: 0xc82000a2bc
goroutine addr before append, B: 0xc82000a2e0
goroutine addr after append, A: 0xc82000a2bc
goroutine addr after append, B: 0xc82000a2e0
{[23] [23]}
After gorountine addr, A: 0xc82000a2bc
After gorountine addr, B: 0xc82000a2e0
After: {[23] [23]}
```

---

<div class="post-metadata">

**Author:** ![Fredo\_Lee](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/f/a88e4f/32.png) [@Fredo\_Lee](https://forum.golangbridge.org/u/Fredo_Lee)\
**Post date:** [March 4, 2016, 5:27am UTC](https://forum.golangbridge.org/t/one-question-when-using-slice/2088/2 "2016-03-04T05:27:57Z")

</div>

as all we know, slice is a reference. Even though the struct is copied, the copied slice refer to the same place…  
so i think it can be changed in other goroutine

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [June 2, 2016, 5:31am UTC](https://forum.golangbridge.org/t/one-question-when-using-slice/2088/3 "2016-06-02T05:31:56Z")

</div>

Closed on June 2, 2016, 5:31am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
