Friday, March 24, 2023
HomeGolangStructure for activities - Code Testimonial

Structure for activities – Code Testimonial


Hey there every person, I’m brand-new to Golang. I intended to check out courses that are made up inactive. So a gamer can stroll() and also fly(), yet probably a beast can just stroll().

In this situation both the stroll() and also fly() techniques require to access the setting of the gamer, so I generated the adhering to code. Nonetheless I ask yourself if this is genuinely Golang colloquial. A point I do not such as is that the exact same guideline is possessed by numerous things.

// You can modify this code!
// Click on this link and also begin inputting.
bundle major.

import "fmt".

kind PositionComponent struct {
x int.
y int.
}

kind Positionable user interface {
GetPositionComponent() * PositionComponent.
SetPositionComponent( x int, y int).
}

func (p * PositionComponent) GetPositionComponent() * PositionComponent {
return p.
}

func (p * PositionComponent) SetPositionComponent( x int, y int) {
p.x = x.
p.y = y.
}

kind Pedestrian struct {
Positionable.
}

kind Leaflet struct {
Positionable.
}

func (w * Pedestrian) stroll() {
w.GetPositionComponent(). x += 1.
fmt.Printf(" Strolled: %sn", w.GetPositionComponent()).
}

func (w * Leaflet) fly() {
w.GetPositionComponent(). y += 1.
fmt.Printf(" Flew: %sn", w.GetPositionComponent()).
}

kind Gamer struct {
* PositionComponent.
Pedestrian.
Leaflet.
}

func major() {
pComp:= PositionComponent {2, 2}
p:= Gamer {&& pComp, Pedestrian {&& pComp}, Leaflet {&& pComp}}
p.walk().
p.fly().
fmt.Println( p.PositionComponent).
}

Go play ground


RELATED ARTICLES

Most Popular

Recent Comments