net/netip and UDP servers

52 views
Skip to first unread message

Juliusz Chroboczek

unread,
Dec 1, 2022, 6:49:01 AM12/1/22
to golan...@googlegroups.com
I'm writing a simple UDP server, and I'm trying to use the new net/netip
data types. My main loop starts as follows:

for {
n, a, err := sock.ReadFrom(buf)
if err != nil {
log.Printf("ReadFrom: %v", err)
time.Sleep(100 * time.Millisecond)
continue
}
aa, ok := a.(*net.UDPAddr)
if !ok {
log.Println("not a UDP address (this shouldn't happen)")
continue
}
ip, ok := netip.AddrFromSlice(aa.IP)
if !ok {
log.Println("AddrFromSlice not ok")
continue
}
ip = ip.Unmap()
addr := netip.AddrPortFrom(ip, uint16(aa.Port))

That seems extremely verbose. Am I doing something wrong?

-- Juliusz

Reply all
Reply to author
Forward
0 new messages