我对下面的 Go 代码感到非常困惑。谁能告诉我为什么
worker=u
并work=&u
有效吗?
worker=p
已验证?
worker=&p
是无效的?
用户和人有什么区别?
package main
import (
"fmt"
)
type Worker interface {
Work()
}
type User struct {
name string
}
func (u User) Work() {
}
type People struct {
name string
}
func (p *People) Work() {
}
func main() {
var worker Worker
u := User{name:"xxx"}
worker = u // valid
worker = &u // valid
p := People{name:"xxx"}
worker = p // invalid
worker = &p // valid
}
慕尼黑8549860
相关分类