Go的编程模式二——funOption

funOption主要用在比较复杂的结构体实例化函数。

package programMod

/*
函数式选项模式
实例化serve通过传入不同的函数来配置不同的参数
*/
import (
    "crypto/tls"
    "time"
)
type Server struct {
    Addr     string        //必填
    Port     int           //必填
    Protocol string        //选填
    Timeout  time.Duration //选填
    MaxConns int           //选填
    TLS      *tls.Config   //选填
}
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
    }
}
func NewServer(addr string, port int, options ...func(*Server)) (*Server, error) {
    srv := Server{
        Addr:     addr,
        Port:     port,
        Protocol: "tcp",
        Timeout:  30 * time.Second,
        MaxConns: 1000,
        TLS:      nil,
    }
    for _, option := range options {
        option(&srv)
    }
    return &srv, nil
}
//通过传入不同的函数来实现配置
// s1, _ := NewServer("localhost", 1024)
// s2, _ := NewServer("localhost", 2048, Protocol("udp"))
// s3, _ := NewServer("0.0.0.0", 8080, Timeout(300*time.Second), MaxConns(1000))
本作品采用《CC 协议》,转载必须注明作者和本文链接
用过哪些工具?为啥用这个工具(速度快,支持高并发...)?底层如何实现的?
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!