@@ -380,14 +380,77 @@ func NewDog(name string, o ...DogOption) Dog {…}
380
380
func NewBird (name string , o ...BirdOption ) Bird {…}
381
381
```
382
382
383
- ### Interface Type
383
+ ### Interfaces
384
384
385
385
To allow other developers to better comprehend the code, it is important
386
386
to ensure it is sufficiently documented. One simple measure that contributes
387
387
to this aim is self-documenting by naming method parameters. Therefore,
388
388
where appropriate, methods of every exported interface type should have
389
389
their parameters appropriately named.
390
390
391
+ #### Interface Stability
392
+
393
+ All exported stable interfaces that include the following warning in their
394
+ doumentation are allowed to be extended with additional methods.
395
+
396
+ > Warning: methods may be added to this interface in minor releases.
397
+
398
+ Otherwise, stable interfaces MUST NOT be modified.
399
+
400
+ If new functionality is needed for an interface that cannot be changed it MUST
401
+ be added by including an additional interface. That added interface can be a
402
+ simple interface for the specific functionality that you want to add or it can
403
+ be a super-set of the original interface. For example, if you wanted to a
404
+ ` Close ` method to the ` Exporter ` interface:
405
+
406
+ ``` go
407
+ type Exporter interface {
408
+ Export ()
409
+ }
410
+ ```
411
+
412
+ A new interface, ` Closer ` , can be added:
413
+
414
+ ``` go
415
+ type Closer interface {
416
+ Close ()
417
+ }
418
+ ```
419
+
420
+ Code that is passed the ` Exporter ` interface can now check to see if the passed
421
+ value also satisfies the new interface. E.g.
422
+
423
+ ``` go
424
+ func caller (e Exporter ) {
425
+ /* ... */
426
+ if c , ok := e.(Closer); ok {
427
+ c.Close ()
428
+ }
429
+ /* ... */
430
+ }
431
+ ```
432
+
433
+ Alternatively, a new type that is the super-set of an ` Exporter ` can be created.
434
+
435
+ ``` go
436
+ type ClosingExporter struct {
437
+ Exporter
438
+ Close ()
439
+ }
440
+ ```
441
+
442
+ This new type can be used similar to the simple interface above in that a
443
+ passed ` Exporter ` type can be asserted to satisfy the ` ClosingExporter ` type
444
+ and the ` Close ` method called.
445
+
446
+ This super-set approach can be useful if there is explicit behavior that needs
447
+ to be coupled with the original type and passed as a unified type to a new
448
+ function, but, because of this coupling, it also limits the applicability of
449
+ the added functionality. If there exist other interfaces where this
450
+ functionality should be added, each one will need their own super-set
451
+ interfaces and will duplicate the pattern. For this reason, the simple targeted
452
+ interface that defines the specific functionality should be preferred.
453
+
391
454
## Approvers and Maintainers
392
455
393
456
Approvers:
0 commit comments