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

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20