Quick actions

cmd+k|ctrl+k

Navigation

Languages

Polymorphism OOP

Snippet info

Language

Go

Visibility

public

Author

riki.whyudi

Created

2023-11-08T01:32:39.940022Z

Updated

2023-11-08T01:32:58.659617Z

package main

import "fmt"

type Shape interface {
	Area() float64
}

type Circle struct {
	Radius float64
}

func NewCircle(radius float64) *Circle {
	return &Circle{Radius: radius}
}

func (c Circle) Area() float64 {
	return 3.14 * c.Radius * c.Radius
}

type Rectangle struct {
	Width  float64
	Height float64
}

func NewRectangle(width, height float64) *Rectangle {
	return &Rectangle{Width: width, Height: height}
}

func (r Rectangle) Area() float64 {
	return r.Width * r.Height
}

func CalculateArea(s Shape) {
	fmt.Printf("Area: %f\n", s.Area())
}

func main() {
	circle := NewCircle(5.0)
	rectangle := NewRectangle(4.0, 6.0)

	CalculateArea(circle)
	CalculateArea(rectangle)
}
INFO