Chat server: Difference between revisions

Content added Content deleted
m (→‎{{header|Go}}: fix minor comment typo)
(→‎{{header|Go}}: fix data race on `s.add` (and move other channel makes just to keep them together))
Line 737: Line 737:
log.Println("Listening for connections on", addr)
log.Println("Listening for connections on", addr)
defer ln.Close()
defer ln.Close()
s := &Server{stop: make(chan bool)}
s := &Server{
add: make(chan *conn),
rem: make(chan string),
msg: make(chan []byte),
stop: make(chan bool),
}
go s.handleConns()
go s.handleConns()
for {
for {
Line 757: Line 762:
// chat client connections as well as broadcasting messages to them.
// chat client connections as well as broadcasting messages to them.
func (s *Server) handleConns() {
func (s *Server) handleConns() {
s.add = make(chan *conn)
s.rem = make(chan string)
s.msg = make(chan string)

// We define the `conns` map here rather than within Server,
// We define the `conns` map here rather than within Server,
// and we use local function literals rather than methods to be
// and we use local function literals rather than methods to be