-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Closed
Description
In the client.go code for the echo example I notice that defer c.Close()
is called both in the main func as well as inside a go func. I am still a go learner and wondering if this is necessary, or an oversight? See "Here" in snippet below. Thanks!
func main() {
flag.Parse()
log.SetFlags(0)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: *addr, Path: "/echo"}
log.Printf("connecting to %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close() // <- **Here**
done := make(chan struct{})
go func() {
defer c.Close() //<- **Here Too!**
defer close(done)
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
}
}()
...
Metadata
Metadata
Assignees
Labels
No labels