Refresher on methods

December 22, 2023

In yesterday’s email, I failed to include the example included in the spec, which doesn’t make sense to include otherwise. I’ve updated the web version of the email in case you’re super curious to see what I left out.


We’ve already covered these details, but they’re logically mentioned again in the section about function calls, so we’ll cover them here:

Calls

A method call x.m() is valid if the method set of (the type of) x contains m and the argument list can be assigned to the parameter list of m. If x is addressable and &x’s method set contains m, x.m() is shorthand for (&x).m():

var p Point
p.Scale(3.5)

There is no distinct method type and there are no method literals.

The main point of interest is this last point: There are no method literals. You can express a function literal:

func() {}

But there’s no way to express a method literal. This implies that you cannot create methods within functions, or create method closures, or add arbitrary methods at runtime, etc.

Quotes from The Go Programming Language Specification Version of August 2, 2023


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Get daily content like this in your inbox!

Subscribe