You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This could be useful if a good example can be worked out. There's some related code in the switch and json examples already, but maybe this would go to interfaces
This could be useful if a good example can be worked out. There's some related code in the switch and json examples already, but maybe this would go to interfaces
I recommend this to go into interfaces. The perimeter of a circle is also called a circumference so a circum() method could be defined on the circle struct that does the same as perim(). Do type assertion against a shape to check if it's circle or rect before calling either perim() or circum()
Example below
// alias for perim()func(ccircle) circum() float64 {
returnc.perim()
}
shapeSlice:= []geometry{rect{width: 5, length: 10}, circle{radius: 5}}
for_, shape:=rangeshapeSlice {
fmt.Println(shape.area())
ifr, ok:=shape.(rect); ok {
fmt.Println(r.perim())
}
ifc, ok:=shape.(circle); ok {
fmt.Println(r.circum())
}
}
Edit: made a PR. The example in PR is simpler than above stated example
Resources:
The text was updated successfully, but these errors were encountered: