When I read the nsqlookupd part of nsq source code, I found that the author used a Context truct to wrap a NSQLookupd struct and no other funcs for Context.I don't know why we use it this way, what benefits we can get by this way? The source code below here. Thank you!
context.go
type Context struct {
nsqlookupd *NSQLookupd
}
nsqlookupd.go
func (l *NSQLookupd) Main() {
ctx := &Context{l}
tcpListener, err := net.Listen("tcp", l.opts.TCPAddress)
if err != nil {
l.logf(LOG_FATAL, "listen (%s) failed - %s", l.opts.TCPAddress, err)
os.Exit(1)
}
l.Lock()
l.tcpListener = tcpListener
l.Unlock()
tcpServer := &tcpServer{ctx: ctx}
l.waitGroup.Wrap(func() {
protocol.TCPServer(tcpListener, tcpServer, l.opts.Logger)
})