面试题答案
一键面试package main
import "fmt"
type Job interface {
Execute()
}
type TaskA struct{}
func (t TaskA) Execute() {
fmt.Println("Executing TaskA")
}
type TaskB struct{}
func (t TaskB) Execute() {
fmt.Println("Executing TaskB")
}
func process(job interface{}) {
if taskA, ok := job.(TaskA); ok {
taskA.Execute()
} else if taskB, ok := job.(TaskB); ok {
taskB.Execute()
}
}