… The type of the expression is the result type of
F. A method invocation is similar but the method itself is specified as a selector upon a value of the receiver type for the method.
math.Atan2(x, y) // function call var pt *Point pt.Scale(3.5) // method call with receiver pt
We recently talked about method calls already, so we don’t need to expand much more on those.
The main thing I want to discuss here is the first sentence: “The type of the expression is the result type of
This is probably intuitive, and we actually demonstrated it yesterday, but it’s worth calling out explicitly, I think.
Given a function type of
func (int, int) int, the type of the expression is the result type
So what about a function type of
func() (int, error)? What is the type of the expression calling it? Well, it’s
(int, error). That is, the expression resolves to multiple values, each of a distinct type (
And this is where the rule we discussed yesterday comes in, that (except for a mystery exception not yet discussed) you can only use a function call as an argument to a function, when it returns a single value only.
Quotes from The Go Programming Language Specification Version of August 2, 2023