选项模式

    1. package main
    2. import "fmt"
    3. type Options struct {
    4. StrOption1 string
    5. StrOption2 string
    6. StrOption3 string
    7. IntOption1 int
    8. IntOption2 int
    9. IntOption3 int
    10. }
    11. func InitOptions1(strOption1 string, strOption2 string, strOption3 string,
    12. IntOption1 int, IntOption2 int, IntOption3 int) {
    13. options := &Options{}
    14. options.StrOption1 = strOption1
    15. options.StrOption2 = strOption2
    16. options.StrOption3 = strOption3
    17. options.IntOption1 = IntOption1
    18. options.IntOption2 = IntOption2
    19. options.IntOption3 = IntOption3
    20. fmt.Printf("init options1:%#v\n", options)
    21. return
    22. }
    23. func InitOptions2(opts ...interface{}) {
    24. options := &Options{}
    25. for index, opt := range opts {
    26. switch index {
    27. case 0:
    28. str, ok := opt.(string)
    29. if !ok {
    30. return
    31. }
    32. options.StrOption1 = str
    33. case 1:
    34. str, ok := opt.(string)
    35. if !ok {
    36. return
    37. }
    38. options.StrOption2 = str
    39. case 2:
    40. str, ok := opt.(string)
    41. if !ok {
    42. return
    43. }
    44. options.StrOption3 = str
    45. case 3:
    46. val, ok := opt.(int)
    47. if !ok {
    48. return
    49. }
    50. options.IntOption1 = val
    51. case 4:
    52. val, ok := opt.(int)
    53. if !ok {
    54. return
    55. }
    56. options.IntOption2 = val
    57. case 5:
    58. val, ok := opt.(int)
    59. if !ok {
    60. return
    61. }
    62. options.IntOption3 = val
    63. }
    64. }
    65. fmt.Printf("init options2:%#v\n", options)
    66. }
    67. type Option func(opts *Options)
    68. func InitOption3(opts ...Option) {
    69. options := &Options{}
    70. for _, opt := range opts {
    71. opt(options)
    72. }
    73. fmt.Printf("init options3:%#v\n", options)
    74. }
    75. func WithStringOption1(str string) Option {
    76. return func(opts *Options) {
    77. opts.StrOption1 = str
    78. }
    79. }
    80. func WithStringOption2(str string) Option {
    81. return func(opts *Options) {
    82. opts.StrOption2 = str
    83. }
    84. }
    85. func WithStringOption3(str string) Option {
    86. return func(opts *Options) {
    87. opts.StrOption3 = str
    88. }
    89. }
    90. func WithIntOption1(val int) Option {
    91. return func(opts *Options) {
    92. opts.IntOption1 = val
    93. }
    94. }
    95. func WithIntOption2(val int) Option {
    96. return func(opts *Options) {
    97. opts.IntOption2 = val
    98. }
    99. }
    100. func WithIntOption3(val int) Option {
    101. return func(opts *Options) {
    102. opts.IntOption3 = val
    103. }
    104. }
    105. func main() {
    106. InitOptions1("str1", "str2", "str3", 1, 2, 3)
    107. InitOptions2("str1", "str2", "str3", 1, 2, 3)
    108. InitOption3(
    109. WithStringOption1("str1"),
    110. WithStringOption3("str3"),
    111. WithIntOption3(3),
    112. WithIntOption2(2),
    113. WithIntOption1(1),
    114. WithStringOption2("str2"),
    115. )
    116. }