的问题是,你使用非指针接收器:
func (b BaseVideo) Download() (err error) {
b.fileContent = bytes.NewReader([]byte("abc"))
return nil
}
这意味着你的Download()
方法获取你调用它的BaseVideo
值的副本。您可以在该方法内修改此副本(您将新的Reader
分配给fileContent
字段),但原始的BaseVideo
将不会被修改。
解决方案:使用指针接收器:
func (b *BaseVideo) Download() (err error) {
b.fileContent = bytes.NewReader([]byte("abc"))
return nil
}
当然,如果你修改了接收器是一个指针,类型BaseVideo
将不再实行Video
接口,只有一个指向BaseVideo
,所以还修改NewBaseVideo
返回指向结构值的指针:*BaseVideo
。您可以通过采取结构字面的地址实现这一点:
func NewBaseVideo(path, name string) Video {
return &BaseVideo{
Path: path,
Name: name,
}
}
优秀的答案。谢谢 – myleftball