I have a list of ports that I need to create a UDP server. I have tried this aproach
for _, r := range routingTable.Routes {
if r.Metric == 0 {
wg.Add(1)
go func() {
conn, err := net.ListenUDP("udp4", &r.OriginPort)
if err != nil {
log.Fatalf("Error occured starting the server: %s", err)
} else {
log.Printf("Listening on port: %s", r.OriginPort.String())
}
defer conn.Close()
buffer := make([]byte, 1024)
for {
conn.ReadFromUDP(buffer)
}
}()
wg.Wait()
}
}
but it is not working. What can I do to make it work ?
wg.Wait()
outside of the loopwg.Done()
from within each goroutine : add a call to defer wg.Done()
in each function(since your listening goroutines never return, the second point is a bit theoretical ... obviously, add some code to have your listening goroutines do something, and exit cleanly if possible)