定义一个结构体如下:
type VideoItem struct {
GroupId int64
ItemId int64
AggrType int32 // Item 还是Group
}
那么如下两个函数有什么区别?
func (item *VideoItem) GetAggrType() int32 {
return item.AggrType
}
func (item VideoItem) GetAggrType() int32 {
return item.AggrType
}
这两个都应该称作方法。
很简单,你是用指针作为接收者,那么变量(或者可以称作对象)本身是按引用传递的,在方法内可以修改对象的数据。
使用值接收者,以为这是按值传递的,那么对象在方法内是处于只读状态的。