Golang type conversion between slice of pointers to slice of a type to slice of another
<p>I am new to Golang.<br>
When I try it I get a compile error:</p>

<pre><code>cannot use a.B (type []*C) as type []Z in field value
</code></pre>

<p>Code:</p>

<pre><code>package main

type A struct {
    B []*C
}

type C struct {
    char string
}

type X struct {
    Y []Z
}

type Z struct {
    char string
}

func doSomething(r interface{}) X {
    a := r.(*A)
    return X{
        Y: a.B, // cannot use a.B (type []*C) as type []Z in field value
    }
}

func main() {
    something := &amp;C{"abc"}
    somewhere := A{}
    somewhere.B = []*C{something}

    doSomething(somewhere)
}
</code></pre>

<p>The way I am thinking of getting around is by iterating over the slice and assigning it to another. But I know there must be other ways to do it.</p>

<p>Go playground: <a href="https://play.golang.org/p/v0PUTPh6Mt" rel="nofollow">https://play.golang.org/p/v0PUTPh6Mt</a></p>