
Go语言中优雅地使用公共结构体作为函数参数
本文探讨如何在Go函数中高效利用公共结构体作为参数,实现代码复用和多态性。
假设我们有一个公共结构体Person:
type Person struct {
// ... fields ...
}
以及两个包含Person字段的结构体Man和Human:
立即学习“go语言免费学习笔记(深入)”;
type Man struct {
Person
// ... Man specific fields ...
}
type Human struct {
Person
// ... Human specific fields ...
}
我们希望定义一个函数ProcessPerson,能够接受Man和Human类型的参数,并操作其公共Person字段。 直接使用Person作为参数类型无法实现这一点,因为Man和Human并非Person的子类型,而是包含Person的组合类型。
方法一:使用接口
我们可以定义一个接口,声明需要的方法,然后让Man和Human实现该接口:
type PersonProcessor interface {
Process(p Person)
}
type Man struct {
Person
// ...
}
func (m *Man) Process(p Person) {
// 处理Person数据
}
type Human struct {
Person
// ...
}
func (h *Human) Process(p Person) {
// 处理Person数据
}
func ProcessPerson(processor PersonProcessor, p Person) {
processor.Process(p)
}
现在,ProcessPerson函数可以接受任何实现了PersonProcessor接口的类型作为第一个参数。
方法二:使用函数作为参数
另一种更灵活的方法是将处理逻辑作为函数参数传入:
type Person struct {
// ... fields ...
}
func ProcessPerson(p Person, processor func(Person)) {
processor(p)
}
func processMan(p Person) {
// 处理Man相关的Person数据
}
func processHuman(p Person) {
// 处理Human相关的Person数据
}
func main() {
man := Man{Person: Person{}}
ProcessPerson(man.Person, processMan) // 传入处理Man的函数
human := Human{Person: Person{}}
ProcessPerson(human.Person, processHuman) // 传入处理Human的函数
}
这种方法避免了定义接口的额外步骤,更直接地将处理逻辑与函数绑定。 选择哪种方法取决于具体需求和代码结构。 如果需要在多个地方复用相同的处理逻辑,接口方法更适合;如果处理逻辑比较特定,函数参数方法更灵活。









