Method expressions, conclusion

Today we’ll finish our discussoin of method expression from Monday and Tuesday. Method expressions … Function values derived from methods are called with function call syntax; the receiver is provided as the first argument to the call. That is, given f := T.Mv, f is invoked as f(t, 7) not t.f(7). We’ve already seen examples of this, the last two days. But to be honest, you’re unlikely to use these types of functions very frequently.


Method expressions, part 2

Today I’ll continue where I left off yesterday, talking about… Method expressions … Similarly, the expression (*T).Mp yields a function value representing Mp with signature func(tp *T, f float32) float32 The key difference to notice between this example and yesterday’s is that the first argument (that of the receiver) is a pointer here, but was not a pointer in the previous example: func(tv T, a int) int // First argument of type T, not a pointer func(tp *T, f float32) float32 // First argument of type *T, a pointer For a method with a value receiver, one can derive a function with an explicit pointer receiver, so


Method expressions

There was no livestream today, as I was a bit under the weather. I intend to be back live streaming next Monday as usual Method expressions If M is in the method set of type T, T.M is a function that is callable as a regular function with the same arguments as M prefixed by an additional argument that is the receiver of the method. MethodExpr = ReceiverType "." MethodName .


Illegal selectors

We’ll cover the last 3 rules of selectors in one go today, since they’re all about illegal selectors. Selectors … In all other cases, x.f is illegal. Simple enough. The cases mentioned in rules 1-3 ar ethe only valid uses of x.f. All others are illegal. var x int x.chicken // illegal! If x is of pointer type and has the value nil and x.f denotes a struct field, assigning to or evaluating x.


Third rule of selectors

Selectors … As an exception, if the type of x is a defined pointer type and (*x).f is a valid selector expression denoting a field (but not a method), x.f is shorthand for (*x).f. I find this wording to be confusing. That probably means some of you do, too. So here goes my best attempt at explaining it. “if the type of x is a defined pointer type” refers to something like this:

How-Tos

8 min watch


Don't mock slog

Don't mock slog! This video shows you the super simple alternative


Second rule of Selectors

Selectors … For a value x of type I where I is an interface type, x.f denotes the actual method with name f of the dynamic value of x. If there is no method with name f in the method set of I, the selector expression is illegal. type I interface { Greet() } type Person struct { Name string } func (p *Person) Greet() { fmt.Printf("Hello, %s", p.Name) } var I x x = &Person{} x.

Subscribe to Boldly Go: Daily

Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.

Unsure? Browse the archive.


The first rule of Selectors

Today I’m live streaming again at the regular time, and I’ll be reviewing your Go code!. I have already received a few submissions, but there’s still time to send yours! And, of course, [join me for the live stream!](https://www.youtube.com/watch?v=pgfyrmWiUCk We're going to talk about Selectors. The spec lays out 6 rules. We’ll start at the beginning, and discuss the first one. Selectors … The following rules apply to selectors: For a value x of type T or *T where T is not a pointer or interface type, x.


Field depth

Selectors … A selector f may denote a field or method f of a type T, or it may refer to a field or method f of a nested embedded field of T. The number of embedded fields traversed to reach f is called its depth in T. The depth of a field or method f declared in T is zero. The depth of a field or method f declared in an embedded field A in T is the depth of f in A plus one.


Selectors

Selectors For a primary expression x that is not a package name, the selector expression x.f denotes the field or method f of the value x (or sometimes *x; see below). The identifier f is called the (field or method) selector; it must not be the blank identifier. The type of the selector expression is the type of f. If x is a package name, see the section on qualified identifiers.


Wanted: Your Go code to review

In next week’s live stream, I’m going to be reviewing Go code from you, the Boldy Go community. I’ll be looking at three or four projects, which I have never seen before, and offering my feedback. And that’s where you come in. I’d love to review your code! Do you have a Go project on GitHub that you’d like to have reviewed? Maybe it’s a take-home coding assignment you did as part of an interview process, and you’d like some more constructive feedback than you got from the recruiter who ghosted you.