在Go编程语言的4.4节(结构体)中,有一段代码摘录:
var dilbert Employee
func EmployeeByID(id int) *Employee { /* ... */ }
id := dilbert.ID
EmployeeByID(id).salary = 0
Run Code Online (Sandbox Code Playgroud)
附注
如果将结果类型
EmployeeByID
更改为,Employee
而不是*Employee
,则赋值语句将无法编译,因为其左侧不会标识变量。
我不明白为什么更改 to 的结果类型EmployeeByID
会Employee
导致 LHS 无法识别变量。
这个简化的示例演示了这个问题:
package main
type t struct {
int
}
func newT() *t { return &t{} }
//func newT() t { return t{} }
func main() {
newT().int = 0
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是,如果您使用的版本newT
不返回指针,并且从不保存对结果的引用newT()
,那么设置其字段的值int
永远不会有意义地执行任何操作。这类似于设置未使用的变量。
如果您使用的是非指针版本,newT
但您有类似的内容:
x := newT()
x.int = 0
Run Code Online (Sandbox Code Playgroud)
那你就没事了。
或者,使用上面的指针版本newT
也可以,因为它可能返回您之前已经定义的某些状态,请参见示例:
package main
type t struct {
int
}
var dilbert = &t{3}
func newT() *t { return dilbert }
//func newT() t { return t{} }
func main() {
println(dilbert.int)
newT().int = 0
println(dilbert.int)
}
Run Code Online (Sandbox Code Playgroud)