I need to implement a server in Golang. I'm using the net package for this purpose but I do not understand how to break out from the accept loop gracefully.
So looking at the example from the net package:
ln, err := net.Listen("tcp", ":8080")
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
}
go handleConnection(conn)
}
I want to do something more along the lines of:
for {
select {
case <-done:
break
case conn, err := <-ln.Accept():
if err != nil {
break
}
...
}
I other words, I want to be able to terminate the program gracefully somehow.