Chat server: Difference between revisions
→{{header|Go}}: language changes: error type and delete function
(add javascript (node.js) solution) |
(→{{header|Go}}: language changes: error type and delete function) |
||
Line 163:
<lang go>package main
import (
"os"
"fmt"
Line 173:
// Quick and dirty error handling.
func
fmt.Printf("Error: %v\n", err)
Line 186:
// A method that makes clientMap compatible with io.Writer, allowing it to be
// used with fmt.Fprintf().
func (cm clientMap) Write(buf []byte) (n int, err
for _, c := range
// Write to each client in a seperate goroutine.
go c.Write(buf)
Line 198:
// Check if a name exists; if it doesn't, add it.
func (cm clientMap) Add(name string, c net.Conn)
for k := range
if name == k {
return false
Line 228:
buf, err := br.ReadBytes('\n')
if err != nil {
return
}
Line 251:
defer func() {
// Remove the client from the list.
delete(clients
}()
Line 267:
switch {
}
Line 282:
func main() {
// Flags. Use -help for usage info.
var (
port int
help bool
Line 298:
lis, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
if err != nil {
}
Line 305:
c, err := lis.Accept()
if err != nil {
continue
}
|