-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
80 lines (67 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"crypto/tls"
"fmt"
"time"
)
// 函数选项:可参考google.golang.org\grpc@v1.27.1\server.go源码 serverOptions
type Server struct {
Addr string // must
Port int // must
Protocol string
Timeout time.Duration
MaxConns int
TLS *tls.Config
}
// 定义一个Option类型的函数,它操作了Server这个对象
type Option func(*Server)
// 下面是对四个可选参数的配置函数
func Protocol(p string) Option {
return func(s *Server) {
s.Protocol = p
}
}
func Timeout(timeout time.Duration) Option {
return func(s *Server) {
s.Timeout = timeout
}
}
func MaxConns(maxconns int) Option {
return func(s *Server) {
s.MaxConns = maxconns
}
}
func TLS(tls *tls.Config) Option {
return func(s *Server) {
s.TLS = tls
}
}
// 实现函数功能
// 用到了不定参数的特性,将任意个option应用到Server上
func NewServer(addr string, port int, options ...Option) (*Server, error) {
// 先填写默认值
srv := Server{
Addr: addr,
Port: port,
Protocol: "tcp",
Timeout: 30 * time.Second,
MaxConns: 1000,
TLS: nil,
}
// 应用任意个option
for _, option := range options {
option(&srv)
}
return &srv, nil
}
func main() {
s1, _ := NewServer("localhost", 1024)
s2, _ := NewServer("localhost", 2048, Protocol("udp"))
s3, _ := NewServer("0.0.0.0", 8080, Timeout(300*time.Second), MaxConns(1000))
fmt.Println(s1, s2, s3)
}
/*
优点:
1. 可读性强,将配置都转化成了对应的函数项option
2. 扩展性好,新增参数只需要增加一个对应的方法
*/