DEV Community

Discussion on: Brief Introduction to Interfaces in Go

Collapse
 
tomxlawson profile image
tomxlawson

This is a clear example of the use of an interface. But, wouldn't it be easier just to replace these lines:

result(a) and result(b)

With these lines?

fmt.Println("Sum: ", a.sum(), "Substr: ", a.substr()) // Sum: 15 Substr: 5
fmt.Println("Sum: ", b.sum(), "Substr: ", b.substr()) // Sum: 30 Substr: 10

Then remove the "math" interface and the "result" function. This would result in less code for this example. But I guess interfaces are still useful if the code is more complex in this.