golang service or structure of the optional parameter assignment

// service structure 
type Server struct { 
    the opts Options // optional parameter variable 
    addr String 
} 

// optional list 
type Options struct { 
    A int 
    B String 
    C BOOL 
    D int 
} 

// optional function parameters assigned 
type ServerOption FUNC (* Options) 

FUNC Afunc (A int ) ServerOption {
     return FUNC (O * Options) { 
        oA = A 
    } 
} 
FUNC Bfunc (B String) ServerOption {
    return func(o *options) {
        o.B = b
    }
}
func Cfunc(c bool) ServerOption {
    return func(o *options) {
        o.C = c
    }
}

//新建服务
func NewServer(addr string, opt ....ServerOption) *Server {
    var opts options
    for _, o := opt {
        o(&options)
    }
    
    return &Server{
        opts: opts,
        addr: addr,
    }
} 
// Example Description 
Server: = NewServer ( " AAAA " , Afunc ( . 1 ), Cfunc ( to true ))

 

Guess you like

Origin www.cnblogs.com/share-ideas/p/11564310.html