Skip to content

Instantly share code, notes, and snippets.

@mmorton
Last active August 29, 2025 01:14
Show Gist options
  • Select an option

  • Save mmorton/8e92b4eb8c5cbb0d27079f3b24f44f03 to your computer and use it in GitHub Desktop.

Select an option

Save mmorton/8e92b4eb8c5cbb0d27079f3b24f44f03 to your computer and use it in GitHub Desktop.
Compose functional options.
package compose
type Composer[T any] func(*T)
func Create[T any, C ~func(*T)](opts ...C) *T {
m := new(T)
Using(m, opts...)
return m
}
func Using[T any, C ~func(*T)](m *T, opts ...C) *T {
for _, opt := range opts {
opt(m)
}
return m
}
func With[T any](fn func(*T)) Composer[T] {
return fn
}
func Combine[T any, C ~func(*T)](opts ...C) Composer[T] {
return func(m *T) {
Using(m, opts...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment