WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Interfaces


Go programming offers every other information type called interfaces which represents a hard and fast of technique signatures. The struct information type implements these interfaces to have approach definitions for the method signature of the interfaces.

Syntax
/* define an interface */
type interface_name interface 
   method_name1 [return_type]
   method_name2 [return_type]
   method_name3 [return_type]
   ...
   Method_namen [return_type]


/* define a struct */
kind struct_name struct 
   /* variables */


/* enforce interface strategies*/
func (struct_name_variable struct_name) method_name1() [return_type] 
   /* technique implementation */

...
Func (struct_name_variable struct_name) method_namen() [return_type] 
   /* technique implementation */


Example

package major

import ("fmt" "math")

/* outline an interface */
kind Shape interface 
   area() float64


/* define a circle */
type Circle struct 
   x,y,radius float64


/* outline a rectangle */
type Rectangle struct 
   width, height float64


/* define a technique for circle (implementation of Shape.Area())*/
func(circle Circle) vicinity() float64 
   go back math.Pi * circle.Radius * circle.Radius


/* outline a way for rectangle (implementation of Shape.Place())*/
func(rect Rectangle) region() float64 
   return rect.Width * rect.Peak


/* outline a technique for shape */
func getArea(shape Shape) float64 
   go back shape.Vicinity()


func predominant() 
   circle := Circlex:0,y:0,radius:5
   rectangle := Rectangle width:10, top:five
   
   fmt.Printf("Circle vicinity: %fn",getArea(circle))
   fmt.Printf("Rectangle vicinity: %fn",getArea(rectangle))


When the above code is compiled and done, it produces the following end result −

Circle region: 78.539816
Rectangle place: 50.000000