Introduction
This challenge aims to test your understanding of struct embedding in Golang. You will be required to create a struct that embeds another struct and access its fields and methods.
This challenge aims to test your understanding of struct embedding in Golang. You will be required to create a struct that embeds another struct and access its fields and methods.
Create a struct named container
that embeds a struct named base
. The base
struct should have a field named num
of type int
and a method named describe()
that returns a string. The container
struct should have a field named str
of type string
. The container
struct should be able to access the num
field and describe()
method of the base
struct.
base
struct should have a field named num
of type int
.base
struct should have a method named describe()
that returns a string.container
struct should have a field named str
of type string
.container
struct should embed the base
struct.container
struct should be able to access the num
field and describe()
method of the base
struct.$ go run struct-embedding.go
co={num: 1, str: some name}
also num: 1
describe: base with num=1
describer: base with num=1
In this challenge, you learned how to embed a struct in another struct and access its fields and methods. You also learned how to bestow interface implementations onto other structs using struct embedding.