Skip to content

Instantly share code, notes, and snippets.

@aybabtme
Created August 11, 2025 17:16
Show Gist options
  • Select an option

  • Save aybabtme/772731b63569130dfe6edb493583e2b6 to your computer and use it in GitHub Desktop.

Select an option

Save aybabtme/772731b63569130dfe6edb493583e2b6 to your computer and use it in GitHub Desktop.
Simple example of making an implementation of an interface that "tees" to other implementations, when chaining/nesting is needed. This does arbitrary nesting.
package teehook
var _ mypkg.Hook = (*Tee)(nil)
func NewTeeHook(hooks ...mypkg.Hook) mypkg.Hook {
return &Tee{hooks: hooks}
}
type Tee struct {
hooks []mypkg.Hook
}
func (tee *Tee) OnXyzHappened(ctx context.Context, blablabla any) error {
for i, hooks := range tee.hooks {
if err := hooks.OnXyzHappened(ctx, blablabla); err != nil {
return fmt.Errorf("tee mypkg %d: %w", i, err)
}
}
return nil
}
func (tee *Tee) OnAbcHappened(ctx context.Context) error {
for i, hooks := range tee.hooks {
if err := hooks.OnAbcHappened(ctx); err != nil {
return fmt.Errorf("tee mypkg %d: %w", i, err)
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment