Files
the-way-to-go_ZH_CN/eBook/examples/chapter_11/interfaces.go
2015-03-03 12:25:25 -05:00

31 lines
515 B
Go

package main
import "fmt"
type Shaper interface {
Area() float32
// Perimeter() float32
}
type Square struct {
side float32
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
func main() {
sq1 := new(Square)
sq1.side = 5
// var areaIntf Shaper
// areaIntf = sq1
// shorter, without separate declaration:
// areaIntf := Shaper(sq1)
// or even:
areaIntf := sq1
fmt.Printf("The square has area: %f\n", areaIntf.Area())
}
// The square has area: 25.000000