Created
August 11, 2025 17:16
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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