Interface method names

April 11, 2023

Basic interfaces

The name of each explicitly specified method must be unique and not blank.

interface {
	String() string
	String() string  // illegal: String not unique
	_(x int)         // illegal: method must have non-blank name

These rules likely look familiar, from our discussion of struct field names, which must also be unique. But there’s a difference: The blank methods are not permitted!

Why is that?

Well, in effect, the blank identifier is write-only. That is, we can name things (types, variables, fields) with the blank identifier, but there’s no way (aside from reflection in a few cases) to reference these.

This means that an interface with a blank identifier would be impossible to use. Nothing could ever match it, and nothing could ever refer to the blank method(s). So, it’s just forbidden. And I’m sure we’re all happier for it!

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

Share this