github.com/goplus/gop@v1.2.6/printer/_testdata/33-Interface/shape.gop (about)

     1  import "math"
     2  
     3  type Shape interface {
     4  	Area() float64
     5  }
     6  
     7  type Rect struct {
     8  	x, y, w, h float64
     9  }
    10  
    11  func (p *Rect) Area() float64 {
    12  	return p.w * p.h
    13  }
    14  
    15  type Circle struct {
    16  	x, y, r float64
    17  }
    18  
    19  func (p *Circle) Area() float64 {
    20  	return math.Pi * p.r * p.r
    21  }
    22  
    23  func Area(shapes ...Shape) float64 {
    24  	s := 0.0
    25  	for shape <- shapes {
    26  		s += shape.Area()
    27  	}
    28  	return s
    29  }
    30  
    31  rect := &Rect{0, 0, 2, 5}
    32  circle := &Circle{0, 0, 3}
    33  println("area:", Area(circle, rect))