Skip to content

Files

Latest commit

 

History

History
53 lines (40 loc) · 685 Bytes

interface.md

File metadata and controls

53 lines (40 loc) · 685 Bytes

Interface implementation

We have two types that implement the shape interface, because but have de method area() float32

package main

import (
	"fmt"
	"math"
)

type shape interface {
	area() float32
}

func info(sh shape) {
	fmt.Printf("the area is %.2f of type %T\n", sh.area(), sh)
}

type squared struct {
	length int
	width  int
}

func (s squared) area() float32 {
	return float32(s.length * s.width)
}

type circle struct {
	radius float32
}

func (c circle) area() float32 {
	return math.Pi * c.radius
}

func main() {
	s := squared{
		length: 2,
		width:  3,
	}
	c := circle{
		radius: 4,
	}

	info(s)
	info(c)

}

Code: https://go.dev/play/p/pRuKV2SskQA