Advertisement
Guest User

Untitled

a guest
Sep 10th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.60 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math"
  6. )
  7.  
  8. type Abser interface {
  9.     Abs() float64
  10. }
  11.  
  12. type MyFloat float64
  13.  
  14. func (f MyFloat) Abs() float64 {
  15.     if f < 0 {
  16.         return float64(-f)
  17.     }
  18.     return float64(f)
  19. }
  20.  
  21. type Vertex struct {
  22.     X, Y float64
  23. }
  24.  
  25. func (v *Vertex) Abs() float64 {
  26.     return math.Sqrt(v.X*v.X + v.Y*v.Y)
  27. }
  28.  
  29. func main() {
  30.     var a Abser
  31.     f := MyFloat(-math.Sqrt2)
  32.     v := Vertex{3, 4}
  33.  
  34.     a = f  // a MyFloat implements Abser
  35.     a = &v // a *Vertex implements Abser
  36.  
  37.     // In the following line, v is a Vertex (not *Vertex)
  38.     // and does NOT implement Abser.
  39.     a = v
  40.  
  41.     fmt.Println(a.Abs())
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement