File tree Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ # Go可变参数
2+
3+ # 一、这是什么
4+ 这个库为Go中的可变参数提供了一些辅助方法,以便更爽的使用可变参数。
5+
6+ # 二、安装
7+ ``` bash
8+ go get -u github.com/golang-infrastructure/go-variable-parameter
9+ ```
10+
11+ # 三、Example
12+
13+ ``` go
14+ package main
15+
16+ import (
17+ " fmt"
18+ variable_parameter " github.com/golang-infrastructure/go-variable-parameter"
19+ )
20+
21+ type FooOptions struct {
22+ Foo string
23+ Bar int
24+ }
25+
26+ var DefaultFooOptions = FooOptions {
27+ Foo : " default foo" ,
28+ Bar : 10 ,
29+ }
30+
31+ func Foo (optionsVariableParams ...FooOptions ) {
32+ // 如果传递了options则使用传递的,如果没传递则使用默认的
33+ options := variable_parameter.TakeFirstParamOrDefault (optionsVariableParams, DefaultFooOptions)
34+ fmt.Println (options.Foo )
35+ }
36+
37+ func main () {
38+
39+ // 不传递参数
40+ Foo ()
41+
42+ // 传递参数
43+ Foo (FooOptions{Foo: " custom foo" })
44+
45+ }
46+ ```
47+
48+
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ variable_parameter "github.com/golang-infrastructure/go-variable-parameter"
6+ )
7+
8+ type FooOptions struct {
9+ Foo string
10+ Bar int
11+ }
12+
13+ var DefaultFooOptions = FooOptions {
14+ Foo : "default foo" ,
15+ Bar : 10 ,
16+ }
17+
18+ func Foo (optionsVariableParams ... FooOptions ) {
19+ // 如果传递了options则使用传递的,如果没传递则使用默认的
20+ options := variable_parameter .TakeFirstParamOrDefault (optionsVariableParams , DefaultFooOptions )
21+ fmt .Println (options .Foo )
22+ }
23+
24+ func main () {
25+
26+ // 不传递参数
27+ Foo ()
28+
29+ // 传递参数
30+ Foo (FooOptions {Foo : "custom foo" })
31+
32+ }
You can’t perform that action at this time.
0 commit comments