Implementing interfaces

Basic interfaces … More than one type may implement an interface. For instance, if two types S1 and S2 have the method set func (p T) Read(p []byte) (n int, err error) func (p T) Write(p []byte) (n int, err error) func (p T) Close() error (where T stands for either S1 or S2) then the File interface is implemented by both S1 and S2, regardless of what other methods S1 and S2 may have or share.


Interface method names

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.


Basic interfaces

Basic interfaces In its most basic form an interface specifies a (possibly empty) list of methods. The type set defined by such an interface is the set of types which implement all of those methods, and the corresponding method set consists exactly of the methods specified by the interface. Interfaces whose type sets can be defined entirely by a list of methods are called basic interfaces. // A simple File interface.


Interface and methods

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.

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.


Duck typing

Interface types An interface type defines a type set. A variable of interface type can store a value of any type that is in the type set of the interface. Such a type is said to implement the interface. The value of an uninitialized variable of interface type is nil. InterfaceType = "interface" "{" { InterfaceElem ";" } "}" . InterfaceElem = MethodElem | TypeElem . MethodElem = MethodName Signature .


Variadic functions

Generally speaking, Go doesn’t allow for the use of optional function parameters. But there is one exception, and that’s where variadic functions come into play. Function types The final incoming parameter in a function signature may have a type prefixed with .... A function with such a parameter is called variadic and may be invoked with zero or more arguments for that parameter. Probably the most ubiquitous example of a variadic function would be fmt.


Unparenthesized results

Function types … Parameter and result lists are always parenthesized except that if there is exactly one unnamed result it may be written as an unparenthesized type. But more directly: Function parameters must always be wrapped in parenthesis. Function reults must be wrapped in parenthesis if there is more than one result, or if the result is named. Further, though not clear from this wording in the spec, if there are no results, no parenthesis are required, even though if there are no parameters, they are still required.

Other

10 min watch


Reaction to Disturbed's "Sound of Silence"

My tireless attempt to be a legitimate YouTuber.


Collapsing like function parameters

Yesterday we learned that we may omit function parameter and result names in a function type definition. But this poses a small limitation: Function types … If [the name is] absent, each type stands for one item of that type. What does that mean, exactly? Well, when using named parameters and results, we have the option to omit the type for subesquent arguments of the same type. Here’s an example to make it more clear;


Naming function parameters and results

Function types … Within a list of parameters or results, the names (IdentifierList) must either all be present or all be absent. If present, each name stands for one item (parameter or result) of the specified type and all non-blank names in the signature must be unique. There are several things to unpack here. Let’s start with the most intuitive part: The names of function parameters and results must be unique.


Function types

Go has first-class functions. That is to say, functions are treated as any other data type, and can be passed around as arguments, used as struct field types, etc. Function types A function type denotes the set of all functions with the same parameter and result types. The value of an uninitialized variable of function type is nil. FunctionType = "func" Signature . Signature = Parameters [ Result ] . Result = Parameters | Type .