多个if

  1. if !PortCheck(d.Port) {
  2. return common.NewParameterError(ModbusAddress{}, "Port")
  3. }
  4. if IsLessThenZero(d.UnitID) {
  5. return common.NewParameterError(ModbusAddress{}, "UnitID")
  6. }
  7. if net.ParseIP(d.IP) == nil {
  8. return common.NewParameterError(ModbusAddress{}, "IP")
  9. }
  1. switch {
  2. case !PortCheck(d.Port):
  3. return common.NewParameterError(ModbusAddress{}, "Port")
  4. case IsLessThenZero(d.UnitID):
  5. return common.NewParameterError(ModbusAddress{}, "UnitID")
  6. case net.ParseIP(d.IP) == nil:
  7. return common.NewParameterError(ModbusAddress{}, "IP")
  8. }

配置对象

当有有一个配置对象Server,其中部分参数必填,部分参数选填。

  1. type Server struct {
  2. Addr string
  3. Port int
  4. Protocol string
  5. Timeout time.Duration
  6. MaxConns int
  7. TLS *tls.Config
  8. }
  1. func NewDefaultServer(addr string, port int) (*Server, error) {
  2. return &Server{addr, port, "tcp", 30 * time.Second, 100, nil}, nil
  3. }
  4. func NewTLSServer(addr string, port int, tls *tls.Config) (*Server, error) {
  5. return &Server{addr, port, "tcp", 30 * time.Second, 100, tls}, nil
  6. }
  7. func NewServerWithTimeout(addr string, port int, timeout time.Duration) (*Server, error) {
  8. return &Server{addr, port, "tcp", timeout, 100, nil}, nil
  9. }
  10. func NewTLSServerWithMaxConnAndTimeout(addr string, port int, maxconns int, timeout time.Duration, tls *tls.Config) (*Server, error) {
  11. return &Server{addr, port, "tcp", 30 * time.Second, maxconns, tls}, nil
  12. }

build模式

  1. //使用一个builder类来做包装
  2. type ServerBuilder struct {
  3. Server
  4. }
  5. func (sb *ServerBuilder) Create(addr string, port int) *ServerBuilder {
  6. sb.Server.Addr = addr
  7. sb.Server.Port = port
  8. //其它代码设置其它成员的默认值
  9. return sb
  10. }
  11. func (sb *ServerBuilder) WithProtocol(protocol string) *ServerBuilder {
  12. sb.Server.Protocol = protocol
  13. return sb
  14. }
  15. func (sb *ServerBuilder) WithMaxConn( maxconn int) *ServerBuilder {
  16. sb.Server.MaxConns = maxconn
  17. return sb
  18. }
  19. func (sb *ServerBuilder) WithTimeOut( timeout time.Duration) *ServerBuilder {
  20. sb.Server.Timeout = timeout
  21. return sb
  22. }
  23. func (sb *ServerBuilder) WithTLS( tls *tls.Config) *ServerBuilder {
  24. sb.Server.TLS = tls
  25. return sb
  26. }
  27. func (sb *ServerBuilder) Build() (Server) {
  28. return sb.Server
  29. }

使用

  1. sb := ServerBuilder{}
  2. server, err := sb.Create("127.0.0.1", 8080).
  3. WithProtocol("udp").
  4. WithMaxConn(1024).
  5. WithTimeOut(30*time.Second).
  6. Build()

Functional Options

  1. type Option func(*Server)
  2. func Protocol(p string) Option {
  3. return func(s *Server) {
  4. s.Protocol = p
  5. }
  6. }
  7. func Timeout(timeout time.Duration) Option {
  8. return func(s *Server) {
  9. s.Timeout = timeout
  10. }
  11. }
  12. func MaxConns(maxconns int) Option {
  13. return func(s *Server) {
  14. s.MaxConns = maxconns
  15. }
  16. }
  17. func TLS(tls *tls.Config) Option {
  18. return func(s *Server) {
  19. s.TLS = tls
  20. }
  21. }
  22. func NewServer(addr string, port int, options ...func(*Server)) (*Server, error) {
  23. srv := Server{
  24. Addr: addr,
  25. Port: port,
  26. Protocol: "tcp",
  27. Timeout: 30 * time.Second,
  28. MaxConns: 1000,
  29. TLS: nil,
  30. }
  31. for _, option := range options {
  32. option(&srv)
  33. }
  34. //...
  35. return &srv, nil
  36. }

使用:

  1. s1, _ := NewServer("localhost", 1024)
  2. s2, _ := NewServer("localhost", 2048, Protocol("udp"))
  3. s3, _ := NewServer("0.0.0.0", 8080, Timeout(300*time.Second), MaxConns(1000))