In the Go programming language; how can pointers to pointers become useful?
(Why are they not illegal if they are not really useful?)
The usefulness of any data type depends on the problem being solved and on the method used to solve the problem. If a data type does not fit the problem, it simply does not fit the problem - and there is nothing more to it.
The Go programming language (as well as most other programming languages) is based on simple rules that the programmer can use to build new data types. Some of these rules are:
*T
: create a new data type that is a pointer to T[10]T
: an array of Tsstruct { t T; u U ... }
: a structure which contains a T as a componentThe programmer can create complex data types by composing these simple rules. The total number of possible data types exceeds the number of useful data types. Clearly, there exist (and have to exist) data types which aren't useful at all. This is just a natural consequence of the fact that the rules for building new data types are simple.
The type **T
falls into the category of types which are less probable to appear in a program. The fact that it is possible to write *****T
doesn't imply that such a type has to be immensely useful.
And finally, the answer to your question:
The type **T
usually appears in contexts where we want to redirect users of a value of type T
to another value of type T
, but for some reason we do not have access to all users of the value or finding the users would cost too much time:
T
(for some reason)T
to access the value via a pointerT
to another valueIn such a situation, using **T
is natural because it allows us to implement the 3rd step in O(1):
type User_of_T struct {
Value **T
}
// Redirect all users of a particular value of type T
// to another value of type T.
func (u *User_of_T) Redirect(t *T) {
*(u.Value) = t
}