20

Optional parameters, default parameter values and method overloading

 3 years ago
source link: https://yourbasic.org/golang/overload-overwrite-optional-parameter/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Optional parameters, default parameter values and method overloading

yourbasic.org/golang
decision-fork-in-the-road.jpg

By design, Go does not support

  • optional parameters,
  • default parameter values,
  • or method overloading.

Method dispatch is simplified if it doesn’t need to do type matching as well. Experience with other languages told us that having a variety of methods with the same name but different signatures was occasionally useful but that it could also be confusing and fragile in practice. Matching only by name and requiring consistency in the types was a major simplifying decision in Go’s type system. Go FAQ: Why does Go not support overloading of methods and operators?

However, there are

For more on this, see Interfaces in 5 easy steps.

The idiomatic way to emulate optional parameters and method overloading in Go is to write several methods with different names. For example, the sort package has five different functions for sorting a slice:

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK