Skip to content
academia.sh

Course Intermediate

Interfaces, Errors and Generics

Published Updated

By the end of this course

Start course

01

Interfaces

A type never writing anywhere in the source that it satisfies an interface, and the small-interface principle being a consequence of this; the empty interface carrying type information into runtime and the cost of that; the question a type assertion and a type switch ask of a value; contract composition through embedding; the measurable counterpart of defining an interface on the consumer side.

  1. 01 The Interface Concept 4 of sixteen bindings are established in a declaration, 4 at a use site, 8 in the value — the course's binding split is produced here. The same type satisfies four interfaces and its source has zero references to any of them; the small-interface principle is not advice here but an observation measured by the count of satisfying types falling as method count rises. Limiting measurement: a compile-time conformance proof moves that same binding into the declaration, and the compiler stays silent when the proof is missing.
  2. 02 The Empty Interface and `any` The empty interface wants zero methods and every type satisfies zero methods: which type will arrive is never written in the source, and type information is carried inside the value. When the same value is carried by three separate static types, the dynamic type does not change; the cost is that anything placed in the value turns into a pair and cannot be called directly. Limiting measurement: when a nil pointer is placed in the empty interface, the interface itself is not nil.
  3. 03 Type Assertion and Type Switch Two of five values satisfy an interface; the type assertion's two-value form counts this without panicking, the single-value form panics when it fails, and the panic is only read as panic=true. A type switch distributes the same five values into its own branches. Limiting measurement: the same value can fit both a concrete-type branch and an interface branch, and the branch chosen depends on writing order — the lesson runs this with two orderings.
  4. 04 Interface Embedding Embedding combines two interfaces into a single composite contract in the source, and this combination adds no new method at all: the same contract, built once by embedding and once rewritten by hand, wants the same two methods. The minimum method count a type must write to satisfy the composite interface equals the total method count of the embedded interfaces, no more, no fewer. Limiting measurement: embedding only combines the contract, not the implementation — two separate types can each satisfy their own separate contract, yet neither satisfies the composite contract.
  5. 05 Interface Design Consumer-side definition is measured: a four-method type is written without referencing any interface at all, four separate consumers unaware of each other each define their own single-method interface, and the same type satisfies all four — the producer's source has zero references to any of these four names. Limiting measurement: when the same contract is defined under two separate names in two consumers, they appear unable to stand in for each other, yet the type system accepts both directly against the same value.

02

Error Handling

Error being a value and implicitly satisfying the interface, sentinel errors carrying their identity in their value, wrapping preserving the chain while adding context and unwrapping searching the chain for the target, and in which case panic and recovery are legitimate and in which they are misuse.

  1. 01 The Error Interface An error is an ordinary value in Go and implicitly satisfies the error interface: any type that writes Error() string becomes an error without ever saying so in its source. Three types confirm this, their dynamic types read with %T; a value-receiver error's structural equality is shown apart from a pointer-receiver error's identity equality. The limiting measure: when a nil pointer is placed into the error interface, the interface itself is not nil.
  2. 02 Sentinel Errors An error value defined once at the package level has its identity standing not in its text but in itself: another errors.New value carrying the same text does not come out equal when compared with it, because both are two different values of type *errors.errorString. errors.Is gives the same result as ==. The limiting measure: when a sentinel error's identity changes, the caller silently falls into the wrong branch, and no warning is produced.
  3. 03 Error Wrapping and Unwrapping An error wrapped with %w stands as a chain inside the outer error, and errors.Is finds the sentinel by walking this chain layer by layer with Unwrap; when %v is used, even though the same text is produced, the chain breaks and errors.Is can no longer find it. errors.As extracts a specific target type from the chain. The limiting measure: even when the wrapping context's text is wrong, the chain's identity is not broken, and no warning is produced.
  4. 04 Panic and Recovery A function can panic instead of returning an error, and this choice leaves no trace in the signature: of two functions, one returning (int, error) and the other returning only int, one always produces a value while the other can crash at run time. recover only works in the deferred function that calls it directly; if a recovered panic is not converted into an error, the information is lost entirely. No stack trace is printed; only panic=true/false and the recovered value's type are measured.

03

Generics

The type parameter being inferred at the call site, type constraints being declared as interfaces with type sets and unions, the instantiation of type-safe containers sitting in the caller's declaration, and the measurable difference between doing the same job with an interface or with generics.

  1. 01 Generic Functions A generic function's type parameter is never written in the source at all, but the compiler builds it at the call site by inference: the same firstElement source, called three times with int, string, and float64 slices, gives %T three different answers. The limiting measurement shows that an unconstrained type parameter allows almost no operation in the body at all: == and + do not compile, only type-independent operations such as len pass.
  2. 02 Type Constraints A type constraint is an interface too, but it lists a type set, not a method: when the same three constraints are tested with four candidates, IntOnly accepts only int, UnderlyingInt (carrying ~int) also accepts named types whose underlying type is int, and the union ~int|~float64 named Numeric accepts both at once — 1/4, 2/4, 3/4. The limiting measurement shows that an interface listing a type set cannot be used as an ordinary value type, while an interface listing only a method can be both a constraint and a value type.
  3. 03 Generic Data Structures A generic data structure's type parameter is written explicitly in the caller's declaration: Stack[int] and Stack[string] show up in %T as two separate dynamic types, and one cannot substitute for the other. The measurable counterpart of type safety: using an element in the same structure written with the empty interface needs a type assertion, in the generic structure it needs zero. The limiting measurement shows that comparability depends both on the container's own field structure and on the type parameter itself.
  4. 04 When Generics The same total-area computation is built three ways: with the empty interface, three type assertions; with generics, zero, but T is pinned to a single concrete type; with an interface contract, also zero, but different concrete types can sit in the same slice. The limiting measurement shows that once T is pinned to an interface (to accept a heterogeneous collection), generics' gain disappears: element size rises from 8 bytes to 16 bytes, exactly like an ordinary interface value. The course found 4 of sixteen bindings in the declaration, 4 at the use site, 8 inside the value.

Final exam

Interfaces, Errors and Generics — Final Exam

In preparation

This course's question bank is not complete yet. The exam opens here automatically once enough questions are added.

Start typing to search.

↑↓ ↵ Esc navigate · open · close