【发布时间】:2021-01-08 09:06:33
【问题描述】:
来自 C#,这让我很困惑。 在 Go 中,如果我有
type Employee struct {
ID int
Salary int
}
那我就可以了
var tom Employee
tom.Salary = 100
到目前为止一切顺利。那么如果我有一个函数
func employeeByID(id int) Employee {
// do something and return an employee
}
那为什么编译不出来呢?
employeeByID(10).Salary = 100
此外,这似乎编译得很好:
andrew := employeeByID(10)
andrew.Salary = 100
【问题讨论】:
-
您希望
employeeByID(10).Salary = 100做什么?如果该函数返回*Employee是有意义的。 -
出于同样的原因你不能做
5 = 10。为什么要为本身不存储在任何地方的东西赋值? -
C# 也做不到
标签: go