Posts

Showing posts with the label golang

Golang: Interfaces

Image
  Go by Example : Interfaces (All are there go through well ) Interfaces  are named collections of method signatures. package main import ( "fmt" "math" ) Here’s a basic interface for geometric shapes. type geometry interface { area () float64 perim () float64 } For our example we’ll implement this interface on  rect  and  circle  types. type rect struct { width , height float64 } type circle struct { radius float64 } To implement an interface in Go, we just need to implement all the methods in the interface. Here we implement  geometry  on  rect s. func ( r rect ) area () float64 { return r . width * r . height } func ( r rect ) perim () float64 { return 2 * r . width + 2 * r . height } The implementation for  circle s. func ( c circle ) area () float64 { return math . Pi * c . radius * c . radius } func ( c circle ) perim () float6...