似乎将 channel 输出分配给一个指针数组将始终分配最后一个发送到 channel string2。谁能解释这种行为?
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)
}
}
结果是:
result2
result2
红糖糍粑
相关分类