It seems assigning channel output to an array of pointers will always assign the last one sent to the channel string2
. can anyone explain this behavior?
package main
import (
"fmt"
)
type myType struct {
myString string
}
func main() {
queue := make(chan myType, 2)
queue <- myType{myString: "string1"}
queue <- myType{myString: "string2"}
close(queue)
var types []*myType
for element := range queue {
types = append(types, &element)
}
for _, element := range types {
fmt.Println(element.myString)
}
}
results in:
result2
result2