调用结构体字面量的方法

huangapple go评论90阅读模式
英文:

Call struct literal's method

问题

这段代码运行正常:

feedService := postgres.FeedService{}
feeds, err := feedService.GetAllRssFeeds()

但是这段代码给我报错:

feeds, err = postgres.FeedService{}.GetAllRssFeeds()

controllers\feed_controller.go:35: 无法在 postgres.FeedService 字面值上调用指针方法
controllers\feed_controller.go:35: 无法获取 postgres.FeedService 字面值的地址

为什么这两段代码不相等?

这是一个结构声明:

type FeedService struct {

}

func (s *FeedService) GetAllRssFeeds() ([]*quzx.RssFeed, error) {
英文:

This code works fine:

feedService := postgres.FeedService{}
feeds, err := feedService.GetAllRssFeeds()

But this code gives me error:

feeds, err = postgres.FeedService{}.GetAllRssFeeds()

> controllers\feed_controller.go:35: cannot call pointer method on
> postgres.FeedService literal controllers\feed_controller.go:35: cannot
> take the address of postgres.FeedService literal

Why this two pieces of code is not equal ?

Here is a struct declaration:

type FeedService struct {

}

func (s *FeedService) GetAllRssFeeds() ([]*quzx.RssFeed, error) {

答案1

得分: 4

你的FeedService.GetAllRssFeeds()方法具有指针接收器,因此需要一个指向FeedService的指针来调用该方法。

在你的第一个示例中,你使用短变量声明FeedService结构体值存储在一个局部变量中。局部变量是可寻址的,所以当你在此之后写feedService.GetAllRssFeeds()时,编译器会自动取feedService的地址并将其作为接收器值使用。这是一个简写形式:

feeds, err := (&feedService).GetAllRssFeeds()

这在规范:调用中有说明:

> 如果x是可寻址的,并且&x的方法集包含m,那么x.m()(&x).m()的简写形式。

在你的第二个示例中,你没有创建一个局部变量,而是直接使用了一个结构体复合字面量,但它本身不是(自动)可寻址的,所以编译器无法获得一个指向FeedService值的指针来用作接收器,因此无法调用该方法。

请注意,允许显式地获取复合字面量的地址,所以下面的代码也是可以工作的:

feeds, err := (&postgres.FeedService{}).GetAllRssFeeds()

这在规范:复合字面量中有说明:

> 获取复合字面量的地址会生成一个指向使用该字面量的值初始化的唯一变量的指针。

相关问题请参考:

https://stackoverflow.com/questions/42477951/what-is-the-method-set-of-sync-waitgroup/42480671#42480671

https://stackoverflow.com/questions/38481420/calling-a-method-with-a-pointer-receiver-by-an-object-instead-of-a-pointer-to-it/38481697#38481697

英文:

Your FeedService.GetAllRssFeeds() method has pointer receiver, so a pointer to FeedService is needed to call this method.

In your first example you use a short variable declaration to store a FeedService struct value in a local variable. Local variables are addressable, so when you write feedService.GetAllRssFeeds() after that, the compiler will automatically take the address of feedService and use that as the receiver value. It is a shorthand for:

feeds, err := (&feedService).GetAllRssFeeds()

It is in Spec: Calls:

> If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m().

In your second example you don't create a local variable, you just use a struct composite literal, but by itself it is not (automatically) addressable, so the compiler cannot obtain a pointer to FeedService value to be used as the receiver, and hence cannot call the method.

Note that it is allowed to take the address of a composite literal explicitly, so the following also works:

feeds, err := (&postgres.FeedService{}).GetAllRssFeeds()

This is in Spec: Composite literals:

> Taking the address of a composite literal generates a pointer to a unique variable initialized with the literal's value.

See related questions:

https://stackoverflow.com/questions/42477951/what-is-the-method-set-of-sync-waitgroup/42480671#42480671

https://stackoverflow.com/questions/38481420/calling-a-method-with-a-pointer-receiver-by-an-object-instead-of-a-pointer-to-it/38481697#38481697

huangapple
  • 本文由 发表于 2017年3月7日 16:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/42643765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定