Composition is a method employed to write re-usable segments of code. It is achieved when objects are made up of other smaller objects with particular behaviors, in other words, Larger objects with a wider functionality are embedded with smaller objects with specific behaviors. The end goal of composition is the same as that of inheritance, however, instead of inheriting features from a parent class/object, larger objects in this regard are composed of the other objects and can thereby use their functionality.
Since writing effective code in the Go Language revolves majorly around using structs and interfaces, composition is a key takeaway from what the language has to offer. In this article we will discuss the use of the composition of types with structs and interfaces, through type embedding:
Example 1: Composition of Structs
package main
import "fmt"
type details struct {
genre string
genreRating string
reviews string
}
type game struct {
name string
price string
details
}
func (d details) showDetails() {
fmt.Println( "Genre:" , d.genre)
fmt.Println( "Genre Rating:" , d.genreRating)
fmt.Println( "Reviews:" , d.reviews)
}
func (g game) show() {
fmt.Println( "Name: " , g.name)
fmt.Println( "Price:" , g.price)
g.showDetails()
}
func main() {
action := details{ "Action" , "18+" , "mostly positive" }
newGame := game{ "XYZ" , "$125" , action}
newGame.show()
}
|
Output:
Name: XYZ
Price: $125
Genre: Action
Genre Rating: 18+
Reviews: mostly positive
Explanation: In the above code snippet we have created two structs: details and game. The struct details comprises of generic information about games. The struct game is a composite struct, which has fields of its own and those of details as well. This composition has been achieved through type embedding as a result of which the first struct becomes a reusable piece of code.
It is interesting to note that the methods which have been defined on the struct details is accessible to objects of Type game, simply because game is composed of details.
Example 2: Composition through Embedding in Interfaces
In the Go language interfaces are implicitly implemented. That is to say, if methods, which are defined in an interface, are used on objects such as structs, then the struct is said to implement the interface. An interface can be embedded with other interfaces in order to form composite interfaces. If all the interfaces in a composite interface are implemented, then the composite interface is also said to be implemented by that object.
package main
import "fmt"
type purchase interface {
sell()
}
type display interface {
show()
}
type salesman interface {
purchase
display
}
type game struct {
name, price string
gameCollection []string
}
func (t game) sell() {
fmt.Println( "--------------------------------------" )
fmt.Println( "Name:" , t.name)
fmt.Println( "Price:" , t.price)
fmt.Println( "--------------------------------------" )
}
func (t game) show() {
fmt.Println( "The Games available are: " )
for _, name := range t.gameCollection {
fmt.Println(name)
}
fmt.Println( "--------------------------------------" )
}
func shop(s salesman) {
fmt.Println(s)
s.sell()
s.show()
}
func main() {
collection := []string{ "XYZ" ,
"Trial by Code" , "Sea of Rubies" }
game1 := game{ "ABC" , "$125" , collection}
shop(game1)
}
|
Output:
{ABC $125 [XYZ Trial by Code Sea of Rubies]}
--------------------------------------
Name: ABC
Price: $125
--------------------------------------
The Games available are:
XYZ
Trial by Code
Sea of Rubies
--------------------------------------
Explanation: Firstly, we created 2 interfaces purchase and display with their own method prototypes. Then we embedded them in the interface salesman in order to form a composite struct. This demonstrates the use of the concept of composition in Go Lang. In the method shop() we have implemented the salesman interface by passing an object of Type game in the method. Since this method implements the composite interface we have access to the child methods of the two interfaces we had initially declared. In this way, effective go programming can be carried out through a clean reusable code.
Last Updated :
05 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...