diff --git a/README.md b/README.md index 5119d12..494343b 100644 --- a/README.md +++ b/README.md @@ -37,10 +37,10 @@ func Foo(optionsVariableParams ...FooOptions) { func main() { // 不传递参数 - Foo() + Foo() // Output: default foo // 传递参数 - Foo(FooOptions{Foo: "custom foo"}) + Foo(FooOptions{Foo: "custom foo"}) // Output: custom foo } ``` diff --git a/examples/main.go b/examples/main.go index d0e338b..9faae0c 100644 --- a/examples/main.go +++ b/examples/main.go @@ -24,9 +24,9 @@ func Foo(optionsVariableParams ...FooOptions) { func main() { // 不传递参数 - Foo() + Foo() // Output: default foo // 传递参数 - Foo(FooOptions{Foo: "custom foo"}) + Foo(FooOptions{Foo: "custom foo"}) // Output: custom foo } diff --git a/variable_parameter.go b/variable_parameter.go index ffe02e5..6739c5e 100644 --- a/variable_parameter.go +++ b/variable_parameter.go @@ -18,3 +18,11 @@ func TakeFirstParamOrDefault[T any](parameters []T, defaultValue T) T { return defaultValue } } + +func TakeFirstParamOrDefaultFunc[T any](parameters []T, defaultValueFunc func() T) T { + if len(parameters) > 0 { + return parameters[0] + } else { + return defaultValueFunc() + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy