我在 Go 的 youtube 教程中看到的关于以下代码的两个问题。
尝试阅读这段代码,我看到指针结构的片段被分配给 productList... 好的,这很好,但为什么要使用 & 符号来表示 Product?(我知道这是地址)。我知道当你想传递给函数时,struct 确实会用 & 标记(因为它默认按值传递)但即使我知道如果接收器是指针,你也不需要使用 & .. 那么为什么在这里使用 & ?
type Product struct {
ID int
name string
}
var productList = []*Product{
&Product{
ID: 1,
Name: "Latte",
},
&Product{
ID: 2,
Name: "Ice Coffee",
},
}
换句话说,上面和下面有什么区别?
var productList = []*Product{
Product{
ID: 1,
Name: "Latte",
},
Product{
ID: 2,
Name: "Ice Coffee",
},
}
交互式爱情
相关分类