← Back Home

Go: Interface Resolution

  1. til

In Go interface resolution is implicit. If the type you pass in matches what the interface is asking for, it will compile.

type Shape interface {
	Area() float64
}

type Shape2 interface {
	Area() float64
	Diameter() float64
}

type Rectangle struct {
	Width float64
	Height float64
}

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

type Circle struct {
	Radius float64
}

func (c Circle) Area() float64 {
	return math.Pow(c.Radius, 2) * math.Pi
}

func (c Circle) Diameter() float64 {
	return c.Radius*2
}

func (c Circle) Temp() string {
	return ""
}

With these code, we can see that

comments powered by Disqus