Go语言教程之边写边学:使用空接口动态添加结构体成员

示例代码:

package main

import (
	"fmt"
)

type Before struct {
	m string
}

func append(b interface{}) interface{} {
	return struct {
		Before
		n string
	}{b.(Before), "rest"}
}

func main() {
	b := Before{"test"}
	a := append(b)
	fmt.Println(a)
}

输出:

{{test} rest}