...

/

Visibility and Operator Patterns

Visibility and Operator Patterns

This lesson explains the possible uses of operators to gain maximum benefits without violating the visibility pattern.

The visibility pattern and interfaces

In Chapter 2, we saw how the simple Visibility rule dictates the access-mode to types, variables, and functions in Go. Chapter 8 showed how you could make the use of the Factory function mandatory when defining types in separate packages.

Define your interfaces in a high-level package that the other packages in your project depend on. Then they can just pass interface values around.

The operator pattern and interfaces

An operator is a unary or binary function which returns a new object and does not modify its parameters, like + and *. In C++, special infix operators (+, -, *, and so on) can be overloaded to support math-like syntax, but apart from a few special cases Go does not support operator overloading. To overcome this limitation, operators must be simulated with functions. Since Go supports a procedural as well as an object-oriented paradigm, there are two options:

Implement the operators as functions

...