0
0

More than 3 years have passed since last update.

Polymorphism with Interfaces

Last updated at Posted at 2021-03-18

work

package main

import (
    "fmt"
)

type Test interface{
    Run()
//  Debug()
}

type My struct{
    body string
}

func (m My)Run(){
    fmt.Println(m.body)
}

func main() {
    my :=  &My{body: "DOOOOOOOO"}
    fmt.Println("Hello, playground")
    testRun(my)
}

func testRun(t Test){
    t.Run()
}

Hello, playground
DOOOOOOOO

Error

package main

import (
    "fmt"
)

type Test interface{
    Run()
//  Debug()
}

type My struct{
    body string
}

func (m My)Run(){
    fmt.Println(m.body)
}

func main() {
    my :=  &My{body: "DOOOOOOOO"}
    fmt.Println("Hello, playground")
    testRun(my)
}

func testRun(t Test){
    t.Run()
}

./prog.go:23:9: cannot use my (type *My) as type Test in argument to testRun:
    *My does not implement Test (missing Debug method)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0