Restrictions on underlying type terms

General interfaces … In a term of the form ~T, the underlying type of T must be itself, and T cannot be an interface. type MyInt int interface { ~[]byte // the underlying type of []byte is itself ~MyInt // illegal: the underlying type of MyInt is not MyInt ~error // illegal: error is an interface } Once again, the spec examples are pretty well explained. TL;DR; the ~ prefix must always be associated with an underlying data type.


Interface examples with type elements

Let’s look at some interface examples from the spec: General interfaces … // An interface representing only the type int. interface { int } // An interface representing all types with underlying type int. interface { ~int } // An interface representing all types with underlying type int that implement the String method. interface { ~int String() string } // An interface representing an empty type set: there is no type that is both an int and a string.


Interfaces don't contain interfaces

As we learned recently, Go interface elements may contain a type term of a non-interface type. It’s worth re-iterating that these are non-interface types. In particular, as the spec states: General interfaces … By construction, an interface’s type set never contains an interface type. That is to say, that the following is invalid: type interface foo { /* some interface elements */ } type interface bar { foo } Actually, I lied.


"Duck Typing" defined

I’ve already introduced the concept of duck typing, but now we have more or less a formal definition: General interfaces … The quantification “the set of all non-interface types” refers not just to all (non-interface) types declared in the program at hand, but all possible types in all possible programs, and hence is infinite. Similarly, given the set of all non-interface types that implement a particular method, the intersection of the method sets of those types will contain exactly that method, even if all types in the program at hand always pair that method with another method.

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.

Book Reviews

16 min read


What is the Best Book to Learn Go in 2023?

After reading every beginner's book I could find on Go, here is my recommended #1 book to help you learn the language.

Book Reviews

6 min read


Book Review: For the Love of Go

For the absolute beginner to programming, this book will get you started on the right foot.

Book Reviews

6 min read


Book Review: Get Programming with Go

Written for the absolute beginner, this book will leave most wanting much more.

Book Reviews

5 min read


Book Review: The Go Programming Language

A great book, but sorely outdated.

Book Reviews

9 min read


Book Review: Go Fundamentals

Very thorough, perhaps to a fault, this book is probably a great second read for most Go newcomers.

Book Reviews

5 min read


Bonus Book Review: Learn Go with Pocket-Sized Projects

A bonus review of this early-access book, which I'm quite excited about.

Book Reviews

7 min read


Book Review: Go For Beginners

This well-written, quick intro to Go for professional developers has some really strong points, but some important down sides, too.