Interface and methods

April 6, 2023

Interface types

An interface type is specified by a list of interface elements. An interface element is either a method or a type element, where a type element is a union of one or more type terms. A type term is either a single type or a single underlying type.

Prior to Go 1.18, interfaces were made up strictly of methods. The introduction of generics complicated things significantly. As such, we’ll talk first about methods, and get to the concept of type elements in a few days.

What is a method? If you’ve ever used any other object-oriented language, you probably have a pretty good idea. But just to be explicit, in Go, a method is a function defined with a receiver. We’ll get into more details on this in due time. But for now, let’s just show a simple example:

// DoSomething is a method on the Foo type.
func (f Foo) DoSomething() {}

// DoSomethingElse is just function, as it has no receiver.
func DoSomethingElse() {}

Interfaces are concerned only with methods.

Quotes from The Go Programming Language Specification Version of December 15, 2022

Share this