golang - simple interface code
To work with golang interface, it really simple. A simple code could be as follows:
package main
import "fmt"
type Vechical interface {
Accelerate(r float32) bool
}
type Scotter struct {
}
func (b Scotter) Accelerate(r float32) bool {
fmt.Println("Bike accelerating")
return true
}
type Bike struct {
}
func (b Bike) Accelerate(r float32) bool {
fmt.Println("Bike accelerating")
return true
}
type Car struct {
}
func (c Car) Accelerate(r float32) bool {
fmt.Println("Car accelerating")
return true
}
func main() {
target := Scotter{} // Car{} or Bike{}
Test(target)
}
func Test(v Vechical) bool {
return v.Accelerate(10)
}
Comments