Interface examples with type elements

April 26, 2023

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.
interface {
	int
	string
}

These examples should be pretty self-documenting. But let’s consider an imaginary example of the third case, as that may be the most interesting.

Let’s consider the HTTP status constants in the net/http package. Although these are untyped constants, we could easily imagine a custom type for these, perhaps defined in our own package:

type HTTPStatus int

const (
  StatusContinue           = HTTPStatus(100) // RFC 9110, 15.2.1
  StatusSwitchingProtocols = HTTPStatus(101) // RFC 9110, 15.2.2
  StatusProcessing         = HTTPStatus(102) // RFC 2518, 10.1
  StatusEarlyHints         = HTTPStatus(103) // RFC 8297

  StatusOK                   = HTTPStatus(200) // RFC 9110, 15.3.1
  StatusCreated              = HTTPStatus(201) // RFC 9110, 15.3.2
  /* etc ... */
)

// String() returns a text for the HTTP status code. It returns the empty
// string if the code is unknown.
func (s HTTPStatus) String() string {
  return http.StatusText(s)
}

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


Share this

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

Unsure? Browse the archive .

Related Content


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.


Expressions

There are a number of terms that get thrown around, often semi-interchangeably by the less initiated (such as myself). “Declaration”, “definition”, “statement”, … and today’s topic “expressions”, just to name a few. But, at least within the context of the Go spec, most such terms have very specific meanings. Expressions An expression specifies the computation of a value by applying operators and functions to operands. So: type foo int and var foo int are not an expressions.


Go 1.21 has been released!

I’m a couple days late with the news… so you hopefully didn’t hear it here first. But two days ago, Go 1.21 was released. This means the Go spec has changed! Today I’ll highlight the main changes introduced to the spec in Go 1.21. None of the changes substantially impacts topics we’ve already covered in the spec series, so I won’t be going back to re-explain changed parts of the spec we’ve already covered.