Interface related
Package Main
Import ("FMT" "math")
Interface that defines the method
The value of the interface type must implement the method defined inside the
Type Abser interface{Abs () float64}
Type Myfloat float64
Type Vertex struct{X, Y float64}
Myfloat Implementing ABS Method
Func (f myfloat) Abs () float64 {
If f < 0{return float64 (-f)} return Float64 (f)}
*vertex Implementing ABS Method
Func (v *vertex) Abs () float64
{return math. SQRT (v.x*v.x + v.y*v.y)
}
Func describe (i interface{})
{FMT. Printf ("%v,%t\n", I, I)}
Func Main ()
{var A, B abser
F: = Myfloat (-math. SQRT2)
Fmt. Println ("======f=", F)
V: = vertex{3,4}
A = f fmt. Println ("======a.abs=", A.abs ())
b = &v FMT. Println ("======b.abs=", B.abs ())
Null interface, no method is developed to implement, can accept any type
var emptyinterface interface{}
Describe (emptyinterface)
Emptyinterface = 12
Describe (emptyinterface)
Emptyinterface = "Wolfan"
Describe (emptyinterface)
}
Go basics: interface-related