您好,我有 2 个看起来相似的函数,我想创建一个通用函数。我的问题是:我不确定如何传入另一个函数:
func (b *Business) StreamHandler1(sm streams.Stream, p []*types.People) {
guard := make(chan struct{}, b.maxManifestGoRoutines)
for _, person := range p {
guard <- struct{}{} // would block if guard channel is already filled
go func(n *types.People) {
b.PeopleHandler(sm, n)
<-guard
}(person)
}
}
func (b *Business) StreamHandler2(sm streams.Stream, pi []*types.PeopleInfo) {
guard := make(chan struct{}, b.maxManifestGoRoutines)
for _, personInfo := range pi {
guard <- struct{}{} // would block if guard channel is already filled
go func(n *types.PeopleInfo) {
b.PeopleInfoHandler(sm, n)
<-guard
}(personInfo)
}
}
你可以看到它们看起来非常非常相似,所以我想做一个可以传入的通用函数PeopleInfoHandler 和PeopleHandler 。知道如何正确地做到这一点吗?看起来像 Go 的语法我应该能够做这样的事情:
func (b *Business) StreamHandler1(f func(streams.Stream, interface{}), sm streams.Stream, p []*interface{}) {
但这似乎不起作用。关于如何使这个通用的任何想法?