Answer the question
In order to leave comments, you need to log in
Why a custom type in go?
The book Effective Go has an example
type ByteSlice []byte
func (slice ByteSlice) Append(data []byte) []byte {
// Body exactly the same as the Append function defined above.
}
Why declare your type "ByteSlice", you can just write func (slice []byte) ....??
Answer the question
In order to leave comments, you need to log in
A classic example with the area of a figure.
Imagine that you have classes Circle, Square, Rectangle, Triangle. Each calculates the area using their own formula.
Write a method where the list of figures is transferred and it is necessary to calculate their total area?
With the Areaer interface and the "Area() float" type method, this is elementary.
With separate methods, you need a huge switch and know in advance about all possible shapes.
Now imagine that a new requirement has come that the figures have coordinates and it is necessary to calculate the intersecting area? With a type method, this is done quite trivially by chaining calls to the same method on a list of shapes.
In short, to write less code.
it's kind of OOP.
Classes and all.
Everything is in the classroom.
Why do without a class if it is fashionable to make your own class?)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question