Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 1.46 KB

interfaces.md

File metadata and controls

53 lines (40 loc) · 1.46 KB
layout
default

Interfaces in Go

Go supports interfaces in a different way than other programming languages like Java does. Like a struct, an interface is created using the type keyword, followed by a name and the keyword interface. Click here to learn more

type Shape interface {
	// area returns the Square's area by multiplying both sides.
	area() float64
}

// Now in order to "implement" this interface, 
// a type must implement the interface methods defined. For example:
type Square struct {
	x1, y1, x2, y2 float64
}

func (s *Square) area() float64 {
	return math.Abs((s.x2 - s.x1) * (s.y2 - s.y1))
}

func measure(s Shape) float64 {
	return s.area()
}

func main() {
	// create new 5x5 Square
	s := Square{0, 0, 5, 5}
	// print the Square's area via the Shape interface
	fmt.Println(measure(&s)) // 25
}

Output

25

Try It Out | Source code

Contributors

<< Home Page | Previous << Map | Next >> Struct