Nic*_*ood 6

Go不做多态.您必须根据接口以及采用这些接口的函数(而不是方法)重新制作您想要做的事情.

io.Readerio.Writerio.Copy

这是我尝试将你的例子改造成那种风格的尝试.它没有多大意义,但希望它会给你一些工作.

package main

import "fmt"

type A struct {
}

type steps interface {
    step1()
    step2()
}

func (a *A) step1() {
    fmt.Println("step1 A")
}

func (a *A) step2() {
    fmt.Println("get A")
}

type B struct {
    A
}

func (b *B) step2() {
    fmt.Println("get B")
}

func step1(f steps) {
    f.step1()
    f.step2()
}

func main() {
    obj := B{}
    step1(&obj)
}