github.com/outbrain/consul@v1.4.5/types/README.md (about)

     1  # Consul `types` Package
     2  
     3  The Go language has a strong type system built into the language.  The
     4  `types` package corrals named types into a single package that is terminal in
     5  `go`'s import graph.  The `types` package should not have any downstream
     6  dependencies.  Each subsystem that defines its own set of types exists in its
     7  own file, but all types are defined in the same package.
     8  
     9  # Why
    10  
    11  > Everything should be made as simple as possible, but not simpler.
    12  
    13  `string` is a useful container and underlying type for identifiers, however
    14  the `string` type is effectively opaque to the compiler in terms of how a
    15  given string is intended to be used.  For instance, there is nothing
    16  preventing the following from happening:
    17  
    18  ```go
    19  // `map` of Widgets, looked up by ID
    20  var widgetLookup map[string]*Widget
    21  // ...
    22  var widgetID string = "widgetID"
    23  w, found := widgetLookup[widgetID]
    24  
    25  // Bad!
    26  var widgetName string = "name of widget"
    27  w, found := widgetLookup[widgetName]
    28  ```
    29  
    30  but this class of problem is entirely preventable:
    31  
    32  ```go
    33  type WidgetID string
    34  var widgetLookup map[WidgetID]*Widget
    35  var widgetName
    36  ```
    37  
    38  TL;DR: intentions and idioms aren't statically checked by compilers.  The
    39  `types` package uses Go's strong type system to prevent this class of bug.